GCC Code Coverage Report


Directory: ./
File: sql/item.cc
Date: 2022-12-13 11:44:05
Exec Total Coverage
Lines: 4829 5326 90.7%
Branches: 3734 5659 66.0%

Line Branch Exec Source
1 /*
2 Copyright (c) 2000, 2022, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include "sql/item.h"
25
26 #include "integer_digits.h"
27 #include "my_compiler.h"
28 #include "my_config.h"
29
30 #include <stdio.h>
31 #ifdef HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #endif
34 #include <stddef.h>
35 #include <algorithm>
36 #include <optional>
37 #include <utility>
38
39 #include "decimal.h"
40 #include "float.h"
41 #include "limits.h"
42 #include "my_alloc.h"
43 #include "my_dbug.h"
44 #include "my_inttypes.h"
45 #include "my_macros.h"
46 #include "myisampack.h" // mi_int8store
47 #include "mysql.h" // IS_NUM
48 #include "mysql_time.h"
49 #include "sql-common/json_dom.h" // Json_wrapper
50 #include "sql/aggregate_check.h" // Distinct_check
51 #include "sql/auth/auth_acls.h"
52 #include "sql/auth/auth_common.h" // get_column_grant
53 #include "sql/auth/sql_security_ctx.h"
54 #include "sql/current_thd.h"
55 #include "sql/derror.h" // ER_THD
56 #include "sql/error_handler.h" // Internal_error_handler
57 #include "sql/gis/srid.h"
58 #include "sql/item_cmpfunc.h" // COND_EQUAL
59 #include "sql/item_create.h" // create_temporal_literal
60 #include "sql/item_func.h" // item_func_sleep_init
61 #include "sql/item_json_func.h" // json_value
62 #include "sql/item_row.h"
63 #include "sql/item_strfunc.h" // Item_func_conv_charset
64 #include "sql/item_subselect.h"
65 #include "sql/item_sum.h" // Item_sum
66 #include "sql/key.h"
67 #include "sql/log_event.h" // append_query_string
68 #include "sql/mysqld.h" // lower_case_table_names files_charset_info
69 #include "sql/protocol.h"
70 #include "sql/query_options.h"
71 #include "sql/select_lex_visitor.h"
72 #include "sql/sp.h" // sp_map_item_type
73 #include "sql/sp_rcontext.h" // sp_rcontext
74 #include "sql/sql_base.h" // view_ref_found
75 #include "sql/sql_bitmap.h"
76 #include "sql/sql_class.h" // THD
77 #include "sql/sql_derived.h" // Condition_pushdown
78 #include "sql/sql_error.h"
79 #include "sql/sql_lex.h"
80 #include "sql/sql_list.h"
81 #include "sql/sql_show.h" // append_identifier
82 #include "sql/sql_time.h" // Date_time_format
83 #include "sql/sql_view.h" // VIEW_ANY_ACL
84 #include "sql/system_variables.h"
85 #include "sql/thd_raii.h"
86 #include "sql/tztime.h" // my_tz_UTC
87 #include "template_utils.h"
88 #include "typelib.h"
89 #include "unsafe_string_append.h"
90
91 using std::max;
92 using std::min;
93 using std::string;
94
95 const String my_null_string("NULL", 4, default_charset_info);
96
97 /**
98 Alias from select list can be referenced only from ORDER BY (SQL Standard) or
99 from HAVING, GROUP BY and a subquery in the select list (MySQL extension).
100
101 We don't allow it be referenced from the SELECT list, with one exception:
102 it's accepted if nested in a subquery, which is inconsistent but necessary
103 as our users have shown to rely on this workaround.
104 */
105 1542 static inline bool select_alias_referencable(enum_parsing_context place) {
106
4/4
✓ Branch 0 taken 807 times.
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 588 times.
✓ Branch 3 taken 219 times.
871 return (place == CTX_SELECT_LIST || place == CTX_GROUP_BY ||
107
4/4
✓ Branch 0 taken 871 times.
✓ Branch 1 taken 671 times.
✓ Branch 2 taken 41 times.
✓ Branch 3 taken 547 times.
2413 place == CTX_HAVING || place == CTX_ORDER_BY);
108 }
109
110 149200 Type_properties::Type_properties(Item &item)
111 149200 : m_type(item.data_type()),
112 149200 m_unsigned_flag(item.unsigned_flag),
113 149200 m_max_length(item.max_length),
114 149200 m_collation(item.collation) {}
115
116 static enum_field_types real_data_type(Item *item);
117
118 /*****************************************************************************
119 ** Item functions
120 *****************************************************************************/
121
122 /**
123 Init all special items.
124 */
125
126 9786 void item_init(void) {
127 9786 item_func_sleep_init();
128 9786 uuid_short_init();
129 9786 }
130
131 66136231 Item::Item()
132 66136572 : next_free(nullptr),
133 66136572 str_value(),
134 66136592 collation(&my_charset_bin, DERIVATION_COERCIBLE),
135 66136828 item_name(),
136 66136820 orig_name(),
137 66136821 max_length(0),
138 66136821 marker(MARKER_NONE),
139 66136821 cmp_context(INVALID_RESULT),
140 66136821 is_parser_item(false),
141 66136821 is_expensive_cache(-1),
142 66136821 m_data_type(MYSQL_TYPE_INVALID),
143 66136821 fixed(false),
144 66136821 decimals(0),
145 66136821 m_nullable(false),
146 66136821 null_value(false),
147 66136821 unsigned_flag(false),
148 66136821 m_is_window_function(false),
149 66136821 derived_used(false),
150 66136231 m_accum_properties(0) {
151 #ifndef NDEBUG
152 66136821 contextualized = true;
153 #endif // NDEBUG
154
155 // Put item into global list so that we can free all items at end
156
2/4
✓ Branch 0 taken 66136905 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 66136963 times.
✗ Branch 3 not taken.
66136821 current_thd->add_item(this);
157 66136963 }
158
159 1791015 Item::Item(THD *thd, const Item *item)
160 1791028 : next_free(nullptr),
161 1791028 str_value(item->str_value),
162 1791041 collation(item->collation),
163 1791041 item_name(item->item_name),
164 1791041 orig_name(item->orig_name),
165 1791041 max_length(item->max_length),
166 1791041 marker(MARKER_NONE),
167 1791041 cmp_context(item->cmp_context),
168 1791041 is_parser_item(false),
169 1791041 is_expensive_cache(-1),
170 1791041 m_data_type(item->data_type()),
171 1791034 fixed(item->fixed),
172 1791034 decimals(item->decimals),
173 1791034 m_nullable(item->m_nullable),
174 1791034 null_value(item->null_value),
175 1791034 unsigned_flag(item->unsigned_flag),
176 1791034 m_is_window_function(item->m_is_window_function),
177 1791034 derived_used(item->derived_used),
178 1791015 m_accum_properties(item->m_accum_properties) {
179 #ifndef NDEBUG
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1791034 times.
1791034 assert(item->contextualized);
181 1791034 contextualized = true;
182 #endif // NDEBUG
183
184 // Add item to global list
185
1/2
✓ Branch 0 taken 1791028 times.
✗ Branch 1 not taken.
1791034 thd->add_item(this);
186 1791028 }
187
188 287609438 Item::Item(const POS &)
189 287609865 : next_free(nullptr),
190 287609865 str_value(),
191 287610231 collation(&my_charset_bin, DERIVATION_COERCIBLE),
192 287610677 item_name(),
193 287610381 orig_name(),
194 287610652 max_length(0),
195 287610652 marker(MARKER_NONE),
196 287610652 cmp_context(INVALID_RESULT),
197 287610652 is_parser_item(true),
198 287610652 is_expensive_cache(-1),
199 287610652 m_data_type(MYSQL_TYPE_INVALID),
200 287610652 fixed(false),
201 287610652 decimals(0),
202 287610652 m_nullable(false),
203 287610652 null_value(false),
204 287610652 unsigned_flag(false),
205 287610652 m_is_window_function(false),
206 287610652 derived_used(false),
207 287609438 m_accum_properties(0) {}
208
209 7536279 bool Item::may_eval_const_item(const THD *thd) const {
210
4/4
✓ Branch 0 taken 122024 times.
✓ Branch 1 taken 7414266 times.
✓ Branch 2 taken 122004 times.
✓ Branch 3 taken 24 times.
7536279 return !thd->lex->is_view_context_analysis() || basic_const_item();
211 }
212
213 /**
214 @todo
215 Make this functions class dependent
216 */
217
218 348094419 bool Item::val_bool() {
219
3/4
✓ Branch 0 taken 348094387 times.
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 356 times.
✗ Branch 3 not taken.
348094419 switch (result_type()) {
220 348094387 case INT_RESULT:
221 348094387 return val_int() != 0;
222 31 case DECIMAL_RESULT: {
223 31 my_decimal decimal_value;
224
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 my_decimal *val = val_decimal(&decimal_value);
225
3/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
31 if (val) return !my_decimal_is_zero(val);
226 6 return false;
227 31 }
228 356 case REAL_RESULT:
229 case STRING_RESULT:
230 356 return val_real() != 0.0;
231 case ROW_RESULT:
232 default:
233 assert(0);
234 return false; // Wrong (but safe)
235 }
236 }
237
238 /*
239 For the items which don't have its own fast val_str_ascii()
240 implementation we provide a generic slower version,
241 which converts from the Item character set to ASCII.
242 For better performance conversion happens only in
243 case of a "tricky" Item character set (e.g. UCS2).
244 Normally conversion does not happen.
245 */
246 138088 String *Item::val_str_ascii(String *str) {
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138088 times.
138088 assert(str != &str_value);
248
249 uint errors;
250
1/2
✓ Branch 0 taken 138106 times.
✗ Branch 1 not taken.
138088 String *res = val_str(&str_value);
251
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 138025 times.
138106 if (!res) return nullptr;
252
253
2/2
✓ Branch 0 taken 137620 times.
✓ Branch 1 taken 402 times.
138025 if (!(res->charset()->state & MY_CS_NONASCII))
254 137620 str = res;
255 else {
256
2/4
✓ Branch 0 taken 401 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 401 times.
402 if ((null_value = str->copy(res->ptr(), res->length(), collation.collation,
257 &my_charset_latin1, &errors)))
258 return nullptr;
259 }
260 138021 return str;
261 }
262
263 12 String *Item::val_string_from_real(String *str) {
264 12 double nr = val_real();
265
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 5 times.
12 if (null_value) return nullptr; /* purecov: inspected */
266 5 str->set_real(nr, decimals, &my_charset_bin);
267 5 return str;
268 }
269
270 21 String *Item::val_string_from_int(String *str) {
271 21 longlong nr = val_int();
272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (null_value) return nullptr;
273 21 str->set_int(nr, unsigned_flag, &my_charset_bin);
274 21 return str;
275 }
276
277 18659 String *Item::val_string_from_decimal(String *str) {
278
1/2
✓ Branch 0 taken 18659 times.
✗ Branch 1 not taken.
18659 my_decimal dec_buf, *dec = val_decimal(&dec_buf);
279
3/4
✓ Branch 0 taken 1800 times.
✓ Branch 1 taken 16859 times.
✓ Branch 2 taken 1800 times.
✗ Branch 3 not taken.
18659 if (null_value) return error_str();
280
1/2
✓ Branch 0 taken 16859 times.
✗ Branch 1 not taken.
16859 my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
281
1/2
✓ Branch 0 taken 16859 times.
✗ Branch 1 not taken.
16859 my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, str);
282 16859 return str;
283 18659 }
284
285 13060 String *Item::val_string_from_datetime(String *str) {
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13060 times.
13060 assert(fixed == 1);
287 MYSQL_TIME ltime;
288
5/6
✓ Branch 0 taken 13060 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2691 times.
✓ Branch 3 taken 10369 times.
✓ Branch 4 taken 10369 times.
✓ Branch 5 taken 2691 times.
15751 if (get_date(&ltime, TIME_FUZZY_DATE) ||
289
2/4
✓ Branch 0 taken 2691 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2691 times.
2691 (null_value = str->alloc(MAX_DATE_STRING_REP_LENGTH)))
290
1/2
✓ Branch 0 taken 10369 times.
✗ Branch 1 not taken.
10369 return error_str();
291
1/2
✓ Branch 0 taken 2691 times.
✗ Branch 1 not taken.
2691 make_datetime((Date_time_format *)nullptr, &ltime, str, decimals);
292 2691 return str;
293 }
294
295 75 String *Item::val_string_from_date(String *str) {
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 assert(fixed == 1);
297 MYSQL_TIME ltime;
298
5/6
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 70 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 70 times.
145 if (get_date(&ltime, TIME_FUZZY_DATE) ||
299
2/4
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 70 times.
70 (null_value = str->alloc(MAX_DATE_STRING_REP_LENGTH)))
300
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 return error_str();
301
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 make_date((Date_time_format *)nullptr, &ltime, str);
302 70 return str;
303 }
304
305 124 String *Item::val_string_from_time(String *str) {
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124 times.
124 assert(fixed == 1);
307 MYSQL_TIME ltime;
308
7/10
✓ Branch 0 taken 124 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 116 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 116 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 116 times.
✓ Branch 8 taken 8 times.
✓ Branch 9 taken 116 times.
124 if (get_time(&ltime) || (null_value = str->alloc(MAX_DATE_STRING_REP_LENGTH)))
309
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 return error_str();
310
1/2
✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
116 make_time((Date_time_format *)nullptr, &ltime, str, decimals);
311 116 return str;
312 }
313
314 35 my_decimal *Item::val_decimal_from_real(my_decimal *decimal_value) {
315
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 DBUG_TRACE;
316
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 double nr = val_real();
317
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 34 times.
35 if (null_value) return nullptr;
318
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
319 34 return decimal_value;
320 35 }
321
322 32840 my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value) {
323 32840 longlong nr = val_int();
324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32840 times.
32840 if (null_value) return nullptr;
325 32840 int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
326 32840 return decimal_value;
327 }
328
329 521 my_decimal *Item::val_decimal_from_string(my_decimal *decimal_value) {
330 String *res;
331
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 521 times.
521 if (!(res = val_str(&str_value))) return nullptr;
333
334
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 474 times.
521 if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM, res->ptr(),
335 res->length(), res->charset(), decimal_value)) {
336 /*
337 The EC_BAD_NUM message is awkward that's why we didn't let
338 str2my_decimal() send it above. We unconditionally send:
339 */
340
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 ErrConvString err(res);
341 47 push_warning_printf(
342
2/4
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
47 current_thd, Sql_condition::SL_WARNING, ER_TRUNCATED_WRONG_VALUE,
343
2/4
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
47 ER_THD(current_thd, ER_TRUNCATED_WRONG_VALUE), "DECIMAL", err.ptr());
344 }
345 521 return decimal_value;
346 }
347
348 84 my_decimal *Item::val_decimal_from_date(my_decimal *decimal_value) {
349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
84 assert(fixed == 1);
350 MYSQL_TIME ltime;
351
3/4
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 54 times.
84 if (get_date(&ltime, TIME_FUZZY_DATE)) {
352 30 return error_decimal(decimal_value);
353 }
354
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 return date2my_decimal(&ltime, decimal_value);
355 }
356
357 71 my_decimal *Item::val_decimal_from_time(my_decimal *decimal_value) {
358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
71 assert(fixed == 1);
359 MYSQL_TIME ltime;
360
3/4
✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 69 times.
71 if (get_time(&ltime)) {
361 2 return error_decimal(decimal_value);
362 }
363
1/2
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
69 return date2my_decimal(&ltime, decimal_value);
364 }
365
366 1533 longlong Item::val_time_temporal() {
367 MYSQL_TIME ltime;
368
3/4
✓ Branch 0 taken 1533 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 1524 times.
1533 if ((null_value = get_time(&ltime))) return 0;
369
1/2
✓ Branch 0 taken 1524 times.
✗ Branch 1 not taken.
1524 return TIME_to_longlong_time_packed(ltime);
370 }
371
372 3582 longlong Item::val_date_temporal() {
373 MYSQL_TIME ltime;
374
1/2
✓ Branch 0 taken 3582 times.
✗ Branch 1 not taken.
3582 const sql_mode_t mode = current_thd->variables.sql_mode;
375 3582 const my_time_flags_t flags =
376 3582 TIME_FUZZY_DATE | (mode & MODE_INVALID_DATES ? TIME_INVALID_DATES : 0) |
377 3582 (mode & MODE_NO_ZERO_IN_DATE ? TIME_NO_ZERO_IN_DATE : 0) |
378 (mode & MODE_NO_ZERO_DATE ? TIME_NO_ZERO_DATE : 0);
379
3/4
✓ Branch 0 taken 3582 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 111 times.
✓ Branch 3 taken 3471 times.
3582 if (get_date(&ltime, flags)) return error_int();
380
1/2
✓ Branch 0 taken 3471 times.
✗ Branch 1 not taken.
3471 return TIME_to_longlong_datetime_packed(ltime);
381 }
382
383 // TS-TODO: split into separate methods?
384 79 longlong Item::val_temporal_with_round(enum_field_types type, uint8 dec) {
385 79 longlong nr = val_temporal_by_field_type();
386 longlong diff =
387 79 my_time_fraction_remainder(my_packed_time_get_frac_part(nr), dec);
388 79 longlong abs_diff = diff > 0 ? diff : -diff;
389
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 77 times.
79 if (abs_diff * 2 >= (int)log_10_int[DATETIME_MAX_DECIMALS - dec]) {
390 /* Needs rounding */
391
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 switch (type) {
392 case MYSQL_TYPE_TIME: {
393 MYSQL_TIME ltime;
394 TIME_from_longlong_time_packed(&ltime, nr);
395 return my_time_adjust_frac(&ltime, dec,
396 current_thd->is_fsp_truncate_mode())
397 ? 0
398 : TIME_to_longlong_time_packed(ltime);
399 }
400 2 case MYSQL_TYPE_TIMESTAMP:
401 case MYSQL_TYPE_DATETIME: {
402 MYSQL_TIME ltime;
403 2 int warnings = 0;
404
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 TIME_from_longlong_datetime_packed(&ltime, nr);
405 6 return propagate_datetime_overflow(
406
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 current_thd, &warnings,
407
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 my_datetime_adjust_frac(&ltime, dec, &warnings,
408
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 current_thd->is_fsp_truncate_mode()))
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 ? 0
410
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 : TIME_to_longlong_datetime_packed(ltime);
411 return nr;
412 }
413 default:
414 assert(0);
415 break;
416 }
417 }
418 /* Does not need rounding, do simple truncation. */
419 77 nr -= diff;
420 77 return nr;
421 }
422
423 103 double Item::val_real_from_decimal() {
424 /* Note that fix_fields may not be called for Item_avg_field items */
425 double result;
426
1/2
✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
103 my_decimal value_buff, *dec_val = val_decimal(&value_buff);
427
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 93 times.
103 if (null_value) return 0.0;
428
1/2
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
93 my_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
429 93 return result;
430 103 }
431
432 39286 double Item::val_real_from_string() {
433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39286 times.
39286 assert(fixed);
434 39286 StringBuffer<STRING_BUFFER_USUAL_SIZE> tmp;
435
1/2
✓ Branch 0 taken 39286 times.
✗ Branch 1 not taken.
39286 const String *res = val_str(&tmp);
436
2/2
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 39084 times.
39286 if (res == nullptr) return 0.0;
437
1/2
✓ Branch 0 taken 39084 times.
✗ Branch 1 not taken.
39084 return double_from_string_with_check(res->charset(), res->ptr(),
438 78168 res->ptr() + res->length());
439 39286 }
440
441 31 longlong Item::val_int_from_decimal() {
442 /* Note that fix_fields may not be called for Item_avg_field items */
443 longlong result;
444
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 my_decimal value, *dec_val = val_decimal(&value);
445
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 28 times.
31 if (null_value) return 0;
446
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 my_decimal2int(E_DEC_FATAL_ERROR, dec_val, unsigned_flag, &result);
447 28 return result;
448 31 }
449
450 30 longlong Item::val_int_from_time() {
451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 assert(fixed == 1);
452 MYSQL_TIME ltime;
453 30 ulonglong value = 0;
454
3/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 26 times.
30 if (get_time(&ltime)) return 0LL;
455
456
3/4
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 17 times.
26 if (current_thd->is_fsp_truncate_mode())
457
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 value = TIME_to_ulonglong_time(ltime);
458 else
459
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 value = TIME_to_ulonglong_time_round(ltime);
460
461
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 return (ltime.neg ? -1 : 1) * value;
462 }
463
464 36 longlong Item::val_int_from_date() {
465
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 assert(fixed == 1);
466 MYSQL_TIME ltime;
467
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 return get_date(&ltime, TIME_FUZZY_DATE)
468
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 32 times.
36 ? 0LL
469
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
36 : (longlong)TIME_to_ulonglong_date(ltime);
470 }
471
472 43 longlong Item::val_int_from_datetime() {
473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 assert(fixed == 1);
474 MYSQL_TIME ltime;
475
2/4
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 43 times.
43 if (get_date(&ltime, TIME_FUZZY_DATE)) return 0LL;
476
477
2/4
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 43 times.
43 if (current_thd->is_fsp_truncate_mode())
478 return TIME_to_ulonglong_datetime(ltime);
479 else {
480
2/4
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
43 return propagate_datetime_overflow(current_thd, [&](int *warnings) {
481 43 return TIME_to_ulonglong_datetime_round(ltime, warnings);
482 43 });
483 }
484 }
485
486 33 longlong Item::val_int_from_string() {
487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 assert(fixed);
488 33 StringBuffer<MY_INT64_NUM_DECIMAL_DIGITS + 1> tmp;
489
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 const String *res = val_str(&tmp);
490
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 19 times.
33 if (res == nullptr) return 0;
491
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 return longlong_from_string_with_check(
492 38 res->charset(), res->ptr(), res->ptr() + res->length(), unsigned_flag);
493 33 }
494
495 1419 type_conversion_status Item::save_time_in_field(Field *field) {
496 MYSQL_TIME ltime;
497
4/6
✓ Branch 0 taken 1419 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 96 times.
✓ Branch 3 taken 1323 times.
✓ Branch 4 taken 96 times.
✗ Branch 5 not taken.
1419 if (get_time(&ltime)) return set_field_to_null_with_conversions(field, false);
498
1/2
✓ Branch 0 taken 1323 times.
✗ Branch 1 not taken.
1323 field->set_notnull();
499
1/2
✓ Branch 0 taken 1323 times.
✗ Branch 1 not taken.
1323 return field->store_time(&ltime, decimals);
500 }
501
502 20800 type_conversion_status Item::save_date_in_field(Field *field) {
503 MYSQL_TIME ltime;
504 20800 my_time_flags_t flags = TIME_FUZZY_DATE;
505
1/2
✓ Branch 0 taken 20800 times.
✗ Branch 1 not taken.
20800 const sql_mode_t mode = current_thd->variables.sql_mode;
506
2/2
✓ Branch 0 taken 4192 times.
✓ Branch 1 taken 16608 times.
20800 if (mode & MODE_INVALID_DATES) flags |= TIME_INVALID_DATES;
507
3/4
✓ Branch 0 taken 20800 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1083 times.
✓ Branch 3 taken 19717 times.
20800 if (get_date(&ltime, flags))
508
1/2
✓ Branch 0 taken 1083 times.
✗ Branch 1 not taken.
1083 return set_field_to_null_with_conversions(field, false);
509
1/2
✓ Branch 0 taken 19717 times.
✗ Branch 1 not taken.
19717 field->set_notnull();
510
1/2
✓ Branch 0 taken 19717 times.
✗ Branch 1 not taken.
19717 return field->store_time(&ltime, decimals);
511 }
512
513 /*
514 Store the string value in field directly
515
516 SYNOPSIS
517 Item::save_str_value_in_field()
518 field a pointer to field where to store
519 result the pointer to the string value to be stored
520
521 DESCRIPTION
522 The method is used by Item_*::save_in_field_inner() implementations
523 when we don't need to calculate the value to store
524 See Item_string::save_in_field_inner() implementation for example
525
526 IMPLEMENTATION
527 Check if the Item is null and stores the NULL or the
528 result value in the field accordingly.
529
530 RETURN
531 Nonzero value if error
532 */
533
534 23755533 type_conversion_status Item::save_str_value_in_field(Field *field,
535 String *result) {
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23755533 times.
23755533 if (null_value) return set_field_to_null(field);
537
538 23755533 field->set_notnull();
539 23755647 return field->store(result->ptr(), result->length(), collation.collation);
540 }
541
542 /**
543 Aggregates data types from array of items into current item
544
545 @param items array of items to aggregate the type from
546
547 This function aggregates all type information from the array of items.
548 Found type is supposed to be used later as the result data type
549 of a multi-argument function.
550 Aggregation itself is performed partially by the Field::field_type_merge()
551 function.
552 */
553
554 7827498 void Item::aggregate_type(Bounds_checked_array<Item *> items) {
555 7827498 uint itemno = 0;
556 7827498 const uint count = items.size();
557
6/6
✓ Branch 0 taken 10701534 times.
✓ Branch 1 taken 95 times.
✓ Branch 2 taken 2874131 times.
✓ Branch 3 taken 7827402 times.
✓ Branch 4 taken 2874131 times.
✓ Branch 5 taken 7827497 times.
10701629 while (itemno < count && items[itemno]->data_type() == MYSQL_TYPE_NULL)
558 2874131 itemno++;
559
560
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 7827403 times.
7827497 if (itemno == count) // All items have NULL type, consolidated type is NULL
561 {
562 94 set_data_type(MYSQL_TYPE_NULL);
563 94 return;
564 }
565
566
2/4
✓ Branch 0 taken 7827403 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7827403 times.
7827403 assert(items[itemno]->result_type() != ROW_RESULT);
567
568
1/2
✓ Branch 0 taken 7827403 times.
✗ Branch 1 not taken.
7827403 enum_field_types new_type = real_data_type(items[itemno]);
569 7827403 uint8 new_dec = items[itemno]->decimals;
570 7827403 bool new_unsigned = items[itemno]->unsigned_flag;
571 7827404 bool mixed_signs = false;
572
573
2/2
✓ Branch 0 taken 6600954 times.
✓ Branch 1 taken 7827404 times.
14428358 for (itemno = itemno + 1; itemno < count; itemno++) {
574 // Do not aggregate items with NULL type
575
2/2
✓ Branch 0 taken 432336 times.
✓ Branch 1 taken 6168618 times.
6600954 if (items[itemno]->data_type() == MYSQL_TYPE_NULL) continue;
576
2/4
✓ Branch 0 taken 6168618 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6168618 times.
6168618 assert(items[itemno]->result_type() != ROW_RESULT);
577
2/4
✓ Branch 0 taken 6168618 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6168618 times.
✗ Branch 3 not taken.
6168618 new_type = Field::field_type_merge(new_type, real_data_type(items[itemno]));
578 6168618 mixed_signs |= (new_unsigned != items[itemno]->unsigned_flag);
579 6168618 new_dec = max<uint8>(new_dec, items[itemno]->decimals);
580 }
581
6/6
✓ Branch 0 taken 1829504 times.
✓ Branch 1 taken 5997900 times.
✓ Branch 2 taken 1810543 times.
✓ Branch 3 taken 18961 times.
✓ Branch 4 taken 1810543 times.
✓ Branch 5 taken 6016861 times.
7827404 if (mixed_signs && is_integer_type(new_type)) {
582 1810543 bool bump_range = false;
583
2/2
✓ Branch 0 taken 3624592 times.
✓ Branch 1 taken 1810543 times.
5435135 for (uint i = 0; i < count; i++)
584
4/4
✓ Branch 0 taken 1810545 times.
✓ Branch 1 taken 1814047 times.
✓ Branch 2 taken 415 times.
✓ Branch 3 taken 1810130 times.
5435552 bump_range |= (items[i]->unsigned_flag &&
585
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 413 times.
1810960 (items[i]->data_type() == new_type ||
586 415 items[i]->data_type() == MYSQL_TYPE_BIT));
587
2/2
✓ Branch 0 taken 1810130 times.
✓ Branch 1 taken 413 times.
1810543 if (bump_range) {
588
5/6
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 1810109 times.
✗ Branch 5 not taken.
1810130 switch (new_type) {
589 5 case MYSQL_TYPE_TINY:
590 5 new_type = MYSQL_TYPE_SHORT;
591 5 break;
592 4 case MYSQL_TYPE_SHORT:
593 4 new_type = MYSQL_TYPE_INT24;
594 4 break;
595 4 case MYSQL_TYPE_INT24:
596 4 new_type = MYSQL_TYPE_LONG;
597 4 break;
598 8 case MYSQL_TYPE_LONG:
599 8 new_type = MYSQL_TYPE_LONGLONG;
600 8 break;
601 1810109 case MYSQL_TYPE_LONGLONG:
602 1810109 new_type = MYSQL_TYPE_NEWDECIMAL;
603 1810109 break;
604 default:
605 break;
606 }
607 }
608 }
609
610 7827404 set_data_type(real_type_to_type(new_type));
611 7827404 decimals = new_dec;
612
4/4
✓ Branch 0 taken 2501875 times.
✓ Branch 1 taken 5325529 times.
✓ Branch 2 taken 746568 times.
✓ Branch 3 taken 1755307 times.
7827404 unsigned_flag = new_unsigned && !mixed_signs;
613 7827404 max_length = 0;
614 7827404 return;
615 }
616
617 288006927 bool Item::itemize(Parse_context *pc, Item **res) {
618
2/2
✓ Branch 0 taken 424753 times.
✓ Branch 1 taken 287582428 times.
288006927 if (skip_itemize(res)) return false;
619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287585311 times.
287582428 if (super::contextualize(pc)) return true;
620
621 // Add item to global list
622 287585311 pc->thd->add_item(this);
623 /*
624 Item constructor can be called during execution other then SQL_COM
625 command => we should check pc->select on zero
626 */
627
1/2
✓ Branch 0 taken 287584457 times.
✗ Branch 1 not taken.
287584387 if (pc->select) {
628 287584457 enum_parsing_context place = pc->select->parsing_place;
629
4/4
✓ Branch 0 taken 117825575 times.
✓ Branch 1 taken 169758882 times.
✓ Branch 2 taken 70739 times.
✓ Branch 3 taken 117754836 times.
287584457 if (place == CTX_SELECT_LIST || place == CTX_HAVING)
630 169829621 pc->select->select_n_having_items++;
631 }
632 287584387 return false;
633 }
634
635 5270886 uint Item::decimal_precision() const {
636 5270886 Item_result restype = result_type();
637 5270900 constexpr const uint DATE_INT_DIGITS{8}; /* YYYYMMDD */
638 5270900 constexpr const uint TIME_INT_DIGITS{7}; /* hhhmmss */
639 5270900 constexpr const uint DATETIME_INT_DIGITS{14}; /* YYYYMMDDhhmmss */
640
641
4/4
✓ Branch 0 taken 4552583 times.
✓ Branch 1 taken 718317 times.
✓ Branch 2 taken 4507206 times.
✓ Branch 3 taken 45377 times.
5270900 if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT)) {
642 5225523 uint prec = my_decimal_length_to_precision(max_char_length(), decimals,
643
1/2
✓ Branch 0 taken 5225523 times.
✗ Branch 1 not taken.
5225523 unsigned_flag);
644 5225525 return max<uint>(1, min<uint>(prec, DECIMAL_MAX_PRECISION));
645 }
646
4/4
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 287 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 44914 times.
45377 switch (data_type()) {
647 151 case MYSQL_TYPE_TIME:
648 151 return decimals + TIME_INT_DIGITS;
649 287 case MYSQL_TYPE_DATETIME:
650 case MYSQL_TYPE_TIMESTAMP:
651 287 return decimals + DATETIME_INT_DIGITS;
652 25 case MYSQL_TYPE_DATE:
653 25 return decimals + DATE_INT_DIGITS;
654 44914 default:
655 44914 break;
656 }
657
1/2
✓ Branch 0 taken 44914 times.
✗ Branch 1 not taken.
44914 return min<uint>(max_char_length(), DECIMAL_MAX_PRECISION);
658 }
659
660 30649 uint Item::time_precision() {
661
2/2
✓ Branch 0 taken 726 times.
✓ Branch 1 taken 495 times.
31870 if (!current_thd->lex->is_view_context_analysis() && const_item() &&
662
8/8
✓ Branch 0 taken 1221 times.
✓ Branch 1 taken 29428 times.
✓ Branch 2 taken 682 times.
✓ Branch 3 taken 44 times.
✓ Branch 4 taken 673 times.
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 673 times.
✓ Branch 7 taken 29976 times.
31870 result_type() == STRING_RESULT && !is_temporal()) {
663 MYSQL_TIME ltime;
664 673 String buf, *tmp;
665 673 MYSQL_TIME_STATUS status;
666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 673 times.
673 assert(fixed);
667 // Nanosecond rounding is not needed, for performance purposes
668
5/6
✓ Branch 0 taken 673 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 666 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 659 times.
✓ Branch 5 taken 14 times.
1339 if ((tmp = val_str(&buf)) &&
669
3/4
✓ Branch 0 taken 666 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 659 times.
✓ Branch 3 taken 7 times.
666 str_to_time(tmp, &ltime, TIME_FRAC_TRUNCATE, &status) == 0)
670 659 return min(status.fractional_digits, uint{DATETIME_MAX_DECIMALS});
671
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 659 times.
673 }
672 29990 return min(decimals, uint8{DATETIME_MAX_DECIMALS});
673 }
674
675 282904 uint Item::datetime_precision() {
676
2/2
✓ Branch 0 taken 170894 times.
✓ Branch 1 taken 105745 times.
559543 if (!current_thd->lex->is_view_context_analysis() && const_item() &&
677
8/8
✓ Branch 0 taken 276639 times.
✓ Branch 1 taken 6265 times.
✓ Branch 2 taken 170841 times.
✓ Branch 3 taken 53 times.
✓ Branch 4 taken 170836 times.
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 170836 times.
✓ Branch 7 taken 112068 times.
559543 result_type() == STRING_RESULT && !is_temporal()) {
678 MYSQL_TIME ltime;
679 170836 String buf, *tmp;
680 170836 MYSQL_TIME_STATUS status;
681
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 170836 times.
170836 assert(fixed);
682 // Nanosecond rounding is not needed, for performance purposes
683
5/6
✓ Branch 0 taken 170836 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 473 times.
✓ Branch 3 taken 170363 times.
✓ Branch 4 taken 466 times.
✓ Branch 5 taken 170370 times.
171309 if ((tmp = val_str(&buf)) &&
684
2/2
✓ Branch 0 taken 466 times.
✓ Branch 1 taken 7 times.
473 !propagate_datetime_overflow(
685
1/2
✓ Branch 0 taken 473 times.
✗ Branch 1 not taken.
473 current_thd, &status.warnings,
686
2/4
✓ Branch 0 taken 473 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 473 times.
✗ Branch 3 not taken.
473 str_to_datetime(tmp, &ltime, TIME_FRAC_TRUNCATE | TIME_FUZZY_DATE,
687 &status)))
688 466 return min(status.fractional_digits, uint{DATETIME_MAX_DECIMALS});
689
2/2
✓ Branch 0 taken 170370 times.
✓ Branch 1 taken 466 times.
170836 }
690 282438 return min(decimals, uint8{DATETIME_MAX_DECIMALS});
691 }
692
693 2453415 void Item::print_item_w_name(const THD *thd, String *str,
694 enum_query_type query_type) const {
695 2453415 print(thd, str, query_type);
696
697
5/6
✓ Branch 0 taken 2453410 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 2453410 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2453410 times.
✓ Branch 5 taken 5 times.
2453415 if (item_name.is_set() && query_type != QT_NORMALIZED_FORMAT) {
698 2453410 str->append(STRING_WITH_LEN(" AS "));
699 2453410 append_identifier(thd, str, item_name.ptr(), item_name.length());
700 }
701 2453415 }
702
703 /**
704 @details
705 "SELECT (subq) GROUP BY (same_subq)" confuses ONLY_FULL_GROUP_BY (it does
706 not see that both subqueries are the same, raises an error).
707 To avoid hitting this problem, if the original query was:
708 "SELECT expression AS x GROUP BY x", we print "GROUP BY x", not
709 "GROUP BY expression". Same for ORDER BY.
710 This has practical importance for views created as
711 "CREATE VIEW v SELECT (subq) AS x GROUP BY x"
712 (print_order() is used to write the view's definition in the frm file).
713 We make one exception: if the view is merge-able, its ORDER clause will be
714 merged into the parent query's. If an identifier in the merged ORDER clause
715 is allowed to be either an alias or an expression of the view's underlying
716 tables, resolution is difficult: it may be to be found in the underlying
717 tables of the view, or in the SELECT list of the view; unlike other ORDER
718 elements directly originating from the parent query.
719 To avoid this problem, if the view is merge-able, we print the
720 expression. This does not cause problems with only_full_group_by, because a
721 merge-able view never has GROUP BY. @see mysql_register_view().
722 */
723 237366 void Item::print_for_order(const THD *thd, String *str,
724 enum_query_type query_type, bool used_alias) const {
725
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 237312 times.
237366 if ((query_type & QT_NORMALIZED_FORMAT) != 0)
726
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 str->append("?");
727
2/2
✓ Branch 0 taken 40996 times.
✓ Branch 1 taken 196316 times.
237312 else if (used_alias) {
728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40996 times.
40996 assert(item_name.is_set());
729 // In the clause, user has referenced expression using an alias; we use it
730 40996 append_identifier(thd, str, item_name.ptr(), item_name.length());
731 } else {
732
5/6
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 196305 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 196305 times.
196316 if (type() == Item::INT_ITEM && basic_const_item()) {
733 /*
734 "ORDER BY N" means "order by the N-th element". To avoid such
735 interpretation we write "ORDER BY ''", which is equivalent.
736 */
737
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 str->append("''");
738 } else
739 196305 print(thd, str, query_type);
740 }
741 237366 }
742
743 3272 bool Item::visitor_processor(uchar *arg) {
744 3272 Select_lex_visitor *visitor = pointer_cast<Select_lex_visitor *>(arg);
745 3272 return visitor->visit(this);
746 }
747
748 /**
749 rename item (used for views, cleanup() return original name).
750
751 @param new_name new name of item;
752 */
753
754 8656960 void Item::rename(char *new_name) {
755 /*
756 we can compare pointers to names here, because if name was not changed,
757 pointer will be same
758 */
759
5/6
✓ Branch 0 taken 8657002 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8657003 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 8656995 times.
✓ Branch 5 taken 8 times.
8656960 if (!orig_name.is_set() && new_name != item_name.ptr()) orig_name = item_name;
760 8657003 item_name.set(new_name);
761 8657045 }
762
763 271991 Item *Item::transform(Item_transformer transformer, uchar *arg) {
764
1/2
✓ Branch 0 taken 271991 times.
✗ Branch 1 not taken.
271991 return (this->*transformer)(arg);
765 }
766
767 59289745 bool Item_ident::itemize(Parse_context *pc, Item **res) {
768
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59289770 times.
59289745 if (skip_itemize(res)) return false;
769
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59289882 times.
59289770 if (super::itemize(pc, res)) return true;
770 59289882 context = pc->thd->lex->current_context();
771 59289860 return false;
772 }
773
774 1 bool Item::check_function_as_value_generator(uchar *checker_args) {
775 Check_function_as_value_generator_parameters *func_arg =
776 1 pointer_cast<Check_function_as_value_generator_parameters *>(
777 checker_args);
778 1 Item_func *func_item = nullptr;
779
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 if (type() == Item::FUNC_ITEM &&
780 ((func_item = down_cast<Item_func *>(this)))) {
781 func_arg->banned_function_name = func_item->func_name();
782 }
783 1 func_arg->err_code = func_arg->get_unnamed_function_error_code();
784 1 return true;
785 }
786
787 72 bool Item_ident::update_depended_from(uchar *arg) {
788 72 auto *info = pointer_cast<Item_ident::Depended_change *>(arg);
789
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 71 times.
72 if (depended_from == info->old_depended_from)
790 1 depended_from = info->new_depended_from;
791 72 return false;
792 }
793
794 /**
795 Store the pointer to this item field into a list if not already there.
796
797 The method is used by Item::walk to collect all unique Item_field objects
798 from a tree of Items into a set of items represented as a list.
799
800 Item_cond::walk() and Item_func::walk() stop the evaluation of the
801 processor function for its arguments once the processor returns
802 true.Therefore in order to force this method being called for all item
803 arguments in a condition the method must return false.
804
805 @param arg pointer to a mem_root_deque<Item_field *>
806
807 @return
808 false to force the evaluation of collect_item_field_processor
809 for the subsequent items.
810 */
811
812 1302198 bool Item_field::collect_item_field_processor(uchar *arg) {
813
1/2
✓ Branch 0 taken 1302198 times.
✗ Branch 1 not taken.
1302198 DBUG_TRACE;
814
6/10
✓ Branch 0 taken 1302198 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1302198 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 1302189 times.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 9 times.
✗ Branch 9 not taken.
1302198 DBUG_PRINT("info", ("%s", field_name ? field_name : "noname"));
815 1302198 mem_root_deque<Item_field *> *item_list =
816 reinterpret_cast<mem_root_deque<Item_field *> *>(arg);
817
7/12
✓ Branch 0 taken 1302198 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1302198 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 898628 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 591140 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1893338 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 898628 times.
✓ Branch 11 taken 994710 times.
1893338 for (Item_field *curr_item : *item_list) {
818
3/4
✓ Branch 0 taken 898628 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 307488 times.
✓ Branch 3 taken 591140 times.
898628 if (curr_item->eq(this, true)) return false; /* Already in the set. */
819 }
820
1/2
✓ Branch 0 taken 994710 times.
✗ Branch 1 not taken.
994710 item_list->push_back(this);
821 994710 return false;
822 1302198 }
823
824 580 bool Item_field::collect_item_field_or_ref_processor(uchar *arg) {
825 580 auto *info = pointer_cast<Collect_item_fields_or_refs *>(arg);
826
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 470 times.
580 if (info->is_stopped(this)) return false;
827
828
1/2
✓ Branch 0 taken 470 times.
✗ Branch 1 not taken.
470 List_iterator<Item> item_list_it(*info->m_items);
829 Item *curr_item;
830
2/2
✓ Branch 0 taken 478 times.
✓ Branch 1 taken 224 times.
702 while ((curr_item = item_list_it++)) {
831
3/4
✓ Branch 0 taken 478 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 246 times.
✓ Branch 3 taken 232 times.
478 if (curr_item->eq(this, true)) return false; /* Already in the set. */
832 }
833
1/2
✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
224 info->m_items->push_back(this);
834 224 return false;
835 }
836
837 406 bool Item_field::collect_item_field_or_view_ref_processor(uchar *arg) {
838 406 auto *info = pointer_cast<Collect_item_fields_or_view_refs *>(arg);
839
2/2
✓ Branch 0 taken 328 times.
✓ Branch 1 taken 78 times.
406 if (info->is_stopped(this)) return false;
840
841
1/2
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
78 List_iterator<Item> item_list_it(*info->m_item_fields_or_view_refs);
842 Item *curr_item;
843
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 37 times.
122 while ((curr_item = item_list_it++)) {
844
3/4
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✓ Branch 3 taken 44 times.
85 if (curr_item->eq(this, true)) return false; /* Already in the set. */
845 }
846
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 info->m_item_fields_or_view_refs->push_back(this);
847 37 return false;
848 }
849
850 553 bool Item_field::add_field_to_set_processor(uchar *arg) {
851
1/2
✓ Branch 0 taken 553 times.
✗ Branch 1 not taken.
553 DBUG_TRACE;
852
3/10
✓ Branch 0 taken 553 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 553 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 553 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
553 DBUG_PRINT("info", ("%s", field->field_name ? field->field_name : "noname"));
853 553 TABLE *table = (TABLE *)arg;
854
2/2
✓ Branch 0 taken 209 times.
✓ Branch 1 taken 344 times.
553 if (table_ref->table == table)
855 209 bitmap_set_bit(&table->tmp_set, field->field_index());
856 553 return false;
857 553 }
858
859 6326219 bool Item_field::add_field_to_cond_set_processor(uchar *) {
860
1/2
✓ Branch 0 taken 6326341 times.
✗ Branch 1 not taken.
6326219 DBUG_TRACE;
861
6/10
✓ Branch 0 taken 6326308 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6326303 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 6326295 times.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 8 times.
✗ Branch 9 not taken.
6326341 DBUG_PRINT("info", ("%s", field->field_name ? field->field_name : "noname"));
862 6326303 bitmap_set_bit(&field->table->cond_set, field->field_index());
863 6326364 return false;
864 6326230 }
865
866 13725939 bool Item_field::remove_column_from_bitmap(uchar *argument) {
867 13725939 MY_BITMAP *bitmap = reinterpret_cast<MY_BITMAP *>(argument);
868 13725939 bitmap_clear_bit(bitmap, field->field_index());
869 13725939 return false;
870 }
871
872 /**
873 Check if an Item_field references some field from a list of fields.
874
875 Check whether the Item_field represented by 'this' references any
876 of the fields in the keyparts passed via 'arg'. Used with the
877 method Item::walk() to test whether any keypart in a sequence of
878 keyparts is referenced in an expression.
879
880 @param arg Field being compared, arg must be of type Field
881
882 @retval
883 true if 'this' references the field 'arg'
884 @retval
885 false otherwise
886 */
887
888 26 bool Item_field::find_item_in_field_list_processor(uchar *arg) {
889 26 KEY_PART_INFO *first_non_group_part = *((KEY_PART_INFO **)arg);
890 26 KEY_PART_INFO *last_part = *(((KEY_PART_INFO **)arg) + 1);
891 KEY_PART_INFO *cur_part;
892
893
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 12 times.
62 for (cur_part = first_non_group_part; cur_part != last_part; cur_part++) {
894
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 36 times.
50 if (field->eq(cur_part->field)) return true;
895 }
896 12 return false;
897 }
898
899 137663 bool Item_field::is_valid_for_pushdown(uchar *arg) {
900 Condition_pushdown::Derived_table_info *dti =
901 137663 pointer_cast<Condition_pushdown::Derived_table_info *>(arg);
902 137663 TABLE_LIST *derived_table = dti->m_derived_table;
903
2/2
✓ Branch 0 taken 134831 times.
✓ Branch 1 taken 2832 times.
137663 if (table_ref == derived_table) {
904
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 134831 times.
134831 assert(field->table == derived_table->table);
905 // If the expression in the derived table for this column has a subquery
906 // or has non-deterministic result or is a trigger field, condition is
907 // not pushed down.
908 // Expressions having subqueries need a more complicated replacement
909 // strategy than the one that currently exists when the condition is
910 // moved to derived table.
911 // TODO: Lift this limitation.
912 // Any condition with expressions having non-deterministic result in the
913 // underlying derived table should not be pushed.
914 // For ex:
915 // select * from (select rand() as a from t1) where a >0.5;
916 // Here a > 0.5 if pushed down would result in rand() getting evaluated
917 // twice because the query would then be
918 // select * from (select rand() as a from t1 where rand() > 0.5) which
919 // is not correct.
920 // Trigger fields need complicated resolving when we clone a condition
921 // having them.
922 // Expressions which have system variables in the underlying derived
923 // table cannot be pushed as of now because Item_func_get_system_var::print
924 // does not print the original expression which leads to an incorrect clone.
925 Query_expression *derived_query_expression =
926 134831 derived_table->derived_query_expression();
927 134831 for (Query_block *qb = derived_query_expression->first_query_block();
928
2/2
✓ Branch 0 taken 135344 times.
✓ Branch 1 taken 134818 times.
270162 qb != nullptr; qb = qb->next_query_block()) {
929
1/2
✓ Branch 0 taken 135344 times.
✗ Branch 1 not taken.
135344 Item *item = qb->get_derived_expr(field->field_index());
930 135344 bool has_trigger_field = false;
931 135344 bool has_system_var = false;
932
1/2
✓ Branch 0 taken 135344 times.
✗ Branch 1 not taken.
135344 WalkItem(item, enum_walk::PREFIX,
933 136184 [&has_trigger_field, &has_system_var](Item *inner_item) {
934
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136184 times.
136184 if (inner_item->type() == Item::TRIGGER_FIELD_ITEM) {
935 has_trigger_field = true;
936 return true;
937 }
938
3/4
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 135834 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 136184 times.
136534 if (inner_item->type() == Item::FUNC_ITEM &&
939
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 350 times.
350 down_cast<Item_func *>(inner_item)->functype() ==
940 Item_func::GSYSVAR_FUNC) {
941 has_system_var = true;
942 return true;
943 }
944 136184 return false;
945 });
946
4/6
✓ Branch 0 taken 135339 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 135331 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 135331 times.
✗ Branch 5 not taken.
270683 if (item->has_subquery() || item->is_non_deterministic() ||
947
5/6
✓ Branch 0 taken 135339 times.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 135331 times.
✓ Branch 4 taken 13 times.
✓ Branch 5 taken 135331 times.
270683 has_trigger_field || has_system_var)
948 13 return true;
949 }
950 134818 return false;
951 }
952 2832 return true;
953 }
954
955 /**
956 Check if this column is found in PARTITION clause of all the window functions.
957 Called when checking to see if a condition can be pushed past window functions
958 while pushing conditions down to materialized derived tables.
959
960 @param arg derived table
961
962 @retval
963 false if this field is part of PARTITION clause of all window functions
964 present in the derived table.
965 @retval
966 true otherwise
967 */
968
969 68 bool Item_field::check_column_in_window_functions(uchar *arg) {
970 68 Query_block *query_block = pointer_cast<Query_block *>(arg);
971 // Find the expression corresponding to this column in derived table's
972 // query block and use that to find in window functions of that
973 // query block.
974
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 Item *item = query_block->get_derived_expr(field->field_index());
975 68 bool ret = true;
976
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 List_iterator<Window> li(query_block->m_windows);
977
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 47 times.
127 for (Window *w = li++; w != nullptr; w = li++) {
978 80 ret = true;
979
3/4
✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
✓ Branch 3 taken 21 times.
111 for (ORDER *o = w->first_partition_by(); o != nullptr; o = o->next) {
980 90 Item *expr = *(o->item);
981
6/8
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 59 times.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 31 times.
✓ Branch 6 taken 59 times.
✓ Branch 7 taken 31 times.
90 if (expr == item || item->eq(expr, false)) {
982 59 ret = false;
983 59 break;
984 }
985 }
986
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 59 times.
80 if (ret) return ret;
987 }
988 47 return ret;
989 }
990
991 /**
992 Check if this column is found in GROUP BY.
993 Called when checking to see if a condition can be pushed past GROUP BY
994 while pushing conditions down to materialized derived tables.
995
996 @param arg derived table
997
998 @retval
999 false if this field is not part of GROUP BY.
1000 @retval
1001 true otherwise.
1002 */
1003 116 bool Item_field::check_column_in_group_by(uchar *arg) {
1004 116 Query_block *query_block = pointer_cast<Query_block *>(arg);
1005 // Find the expression corresponding to this column in the derived
1006 // table's query block and use that to find in GROUP BY of that
1007 // query block.
1008 116 Item *item = query_block->get_derived_expr(field->field_index());
1009
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 47 times.
189 for (ORDER *group = query_block->group_list.first; group;
1010 73 group = group->next) {
1011
5/6
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 73 times.
✓ Branch 4 taken 69 times.
✓ Branch 5 taken 73 times.
142 if (*group->item == item || item->eq(*group->item, false)) return false;
1012 }
1013 47 return true;
1014 }
1015
1016 134179 Item *Item_field::replace_with_derived_expr(uchar *arg) {
1017 Condition_pushdown::Derived_table_info *dti =
1018 134179 pointer_cast<Condition_pushdown::Derived_table_info *>(arg);
1019
1020 // This column's table reference should be same as the derived table from
1021 // where the replacement is retrieved. If not, it is presumed that the
1022 // column has already been replaced with derived table expression (Maybe
1023 // there was an earlier reference to the same column in the condition that
1024 // is being pushed down). There is no need to do anything in such a case.
1025 134179 TABLE_LIST *derived_table = dti->m_derived_table;
1026
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 134179 times.
134179 if (derived_table != table_ref) return this;
1027 134179 Query_block *query_block = dti->m_derived_query_block;
1028 134179 return query_block->clone_expression(
1029 268358 current_thd, query_block->get_derived_expr(field->field_index()),
1030
2/2
✓ Branch 0 taken 134095 times.
✓ Branch 1 taken 84 times.
268274 (derived_table->is_system_view ||
1031
2/2
✓ Branch 0 taken 259 times.
✓ Branch 1 taken 133836 times.
134095 (derived_table->referencing_view &&
1032
2/2
✓ Branch 0 taken 251 times.
✓ Branch 1 taken 8 times.
134438 derived_table->referencing_view->is_system_view)));
1033 }
1034
1035 97 Item *Item_field::replace_with_derived_expr_ref(uchar *arg) {
1036 Condition_pushdown::Derived_table_info *dti =
1037 97 pointer_cast<Condition_pushdown::Derived_table_info *>(arg);
1038
1039 // This column's table reference should be same as the derived table from
1040 // where the replacement is retrieved. If not, it is presumed that the
1041 // column has already been replaced with derived table expression (Maybe
1042 // there was an earlier reference to the same column in the condition that
1043 // is being pushed down). There is no need to do anything in such a case.
1044 97 TABLE_LIST *derived_table = dti->m_derived_table;
1045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 97 times.
97 if (derived_table != table_ref) return this;
1046 97 Query_block *query_block = dti->m_derived_query_block;
1047
1048 // Get the expression in the derived table and find the right ref item to
1049 // point to.
1050 97 Item *select_item = query_block->get_derived_expr(field->field_index());
1051 97 Item *new_ref = nullptr;
1052
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 if (select_item) {
1053 97 uint counter = 0;
1054 enum_resolution_type resolution;
1055
3/6
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 97 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 97 times.
✗ Branch 5 not taken.
97 if (find_item_in_list(current_thd, select_item,
1056 query_block->get_fields_list(), &counter,
1057 REPORT_EXCEPT_NOT_FOUND, &resolution)) {
1058 97 Item **replace_item = &query_block->base_ref_items[counter];
1059
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
194 new_ref = new Item_ref(&query_block->context, replace_item, nullptr,
1060 97 nullptr, (*replace_item)->item_name.ptr(),
1061
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
194 resolution == RESOLVED_AGAINST_ALIAS);
1062 }
1063 }
1064
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 97 times.
97 assert(new_ref);
1065 97 return new_ref;
1066 }
1067
1068 82593 bool Item_field::check_function_as_value_generator(uchar *checker_args) {
1069 Check_function_as_value_generator_parameters *func_args =
1070 82593 pointer_cast<Check_function_as_value_generator_parameters *>(
1071 checker_args);
1072 // We walk through the Item tree twice to check for disallowed functions;
1073 // once before resolving is done and once after resolving is done. Before
1074 // resolving is done, we don't have the field object available, and hence
1075 // the nullptr check.
1076
2/2
✓ Branch 0 taken 7167 times.
✓ Branch 1 taken 75426 times.
82593 if (field == nullptr) {
1077 7167 return false;
1078 }
1079
1080 75426 int fld_idx = func_args->col_index;
1081
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75426 times.
75426 assert(fld_idx > -1);
1082
1083 /*
1084 Don't allow the GC (or default expression) to refer itself or another GC
1085 (or default expressions) that is defined after it.
1086 */
1087
2/2
✓ Branch 0 taken 67630 times.
✓ Branch 1 taken 487 times.
68117 if ((func_args->source != VGS_CHECK_CONSTRAINT) &&
1088
2/2
✓ Branch 0 taken 693 times.
✓ Branch 1 taken 66937 times.
135747 (field->is_gcol() ||
1089
4/4
✓ Branch 0 taken 68117 times.
✓ Branch 1 taken 7309 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 75423 times.
211173 field->has_insert_default_general_value_expression()) &&
1090
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1177 times.
1180 field->field_index() >= fld_idx) {
1091 6 func_args->err_code = (func_args->source == VGS_GENERATED_COLUMN)
1092
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 ? ER_GENERATED_COLUMN_NON_PRIOR
1093 : ER_DEFAULT_VAL_GENERATED_NON_PRIOR;
1094 3 return true;
1095 }
1096 /*
1097 If a generated column, default expression or check constraint depends
1098 on an auto_increment column:
1099 - calculation of the generated value is done before write_row(),
1100 - but the auto_increment value is determined in write_row() by the
1101 engine.
1102 So this case is forbidden.
1103 */
1104
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 75419 times.
75423 if (field->is_flag_set(AUTO_INCREMENT_FLAG)) {
1105 4 func_args->err_code =
1106 4 (func_args->source == VGS_GENERATED_COLUMN)
1107
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
7 ? ER_GENERATED_COLUMN_REF_AUTO_INC
1108 3 : (func_args->source == VGS_DEFAULT_EXPRESSION)
1109
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 ? ER_DEFAULT_VAL_GENERATED_REF_AUTO_INC
1110 : ER_CHECK_CONSTRAINT_REFERS_AUTO_INCREMENT_COLUMN;
1111 4 return true;
1112 }
1113
1114 75419 return false;
1115 }
1116
1117 /**
1118 Check privileges of base table column
1119 */
1120
1121 41847479 bool Item_field::check_column_privileges(uchar *arg) {
1122 41847479 THD *thd = (THD *)arg;
1123
1124 Internal_error_handler_holder<View_error_handler, TABLE_LIST> view_handler(
1125
1/2
✓ Branch 0 taken 41848325 times.
✗ Branch 1 not taken.
41847479 thd, context->view_error_handler, context->view_error_handler_arg);
1126
3/4
✓ Branch 0 taken 41854417 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 135 times.
✓ Branch 3 taken 41854282 times.
41848325 if (check_column_grant_in_table_ref(thd, table_ref, field_name,
1127 strlen(field_name),
1128 thd->want_privilege)) {
1129 135 return true;
1130 }
1131
1132 41854282 return false;
1133 41854417 }
1134
1135 /**
1136 Check privileges of view column.
1137
1138 @note this function will be called for columns from views and derived tables,
1139 however privilege check for derived tables should be skipped
1140 (those columns are checked against the base tables).
1141 */
1142
1143 4171893 bool Item_view_ref::check_column_privileges(uchar *arg) {
1144 4171893 THD *thd = (THD *)arg;
1145
1146
2/2
✓ Branch 0 taken 323199 times.
✓ Branch 1 taken 3848704 times.
4171893 if (cached_table->is_derived()) // Rely on checking underlying tables
1147 323199 return false;
1148
1149 Internal_error_handler_holder<View_error_handler, TABLE_LIST> view_handler(
1150
1/2
✓ Branch 0 taken 3848707 times.
✗ Branch 1 not taken.
3848704 thd, context->view_error_handler, context->view_error_handler_arg);
1151
1152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3848707 times.
3848707 assert(strlen(cached_table->get_table_name()) > 0);
1153
1154
3/4
✓ Branch 0 taken 3848711 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
✓ Branch 3 taken 3848619 times.
3848707 if (check_column_grant_in_table_ref(thd, cached_table, field_name,
1155 strlen(field_name), thd->want_privilege))
1156 92 return true;
1157
1158 3848619 return false;
1159 3848711 }
1160
1161 3595749 bool Item::may_evaluate_const(const THD *thd) const {
1162 // Ensure tables are locked whenever preparation is complete
1163
3/4
✓ Branch 0 taken 424519 times.
✓ Branch 1 taken 3171230 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 424519 times.
3595749 assert(!thd->lex->is_exec_started() || thd->lex->is_query_tables_locked());
1164
2/2
✓ Branch 0 taken 3549976 times.
✓ Branch 1 taken 45773 times.
7145725 return !(thd->lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_VIEW) &&
1165
2/2
✓ Branch 0 taken 3061151 times.
✓ Branch 1 taken 488825 times.
3549976 (const_item() ||
1166
4/4
✓ Branch 0 taken 42244 times.
✓ Branch 1 taken 3018907 times.
✓ Branch 2 taken 17427 times.
✓ Branch 3 taken 24817 times.
6656900 (const_for_execution() && thd->lex->is_exec_started()));
1167 }
1168
1169 174154667 bool Item::check_cols(uint c) {
1170
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 174154641 times.
174154667 if (c != 1) {
1171 26 my_error(ER_OPERAND_COLUMNS, MYF(0), c);
1172 26 return true;
1173 }
1174 174154641 return false;
1175 }
1176
1177 const Name_string null_name_string(nullptr, 0);
1178
1179 66842442 void Name_string::copy(const char *str, size_t length, const CHARSET_INFO *cs) {
1180
2/2
✓ Branch 0 taken 2261398 times.
✓ Branch 1 taken 64581044 times.
66842442 if (!length) {
1181 /* Empty string, used by AS or internal function like last_insert_id() */
1182
2/2
✓ Branch 0 taken 2261382 times.
✓ Branch 1 taken 16 times.
2261398 set(str ? "" : nullptr, 0);
1183 2261415 return;
1184 }
1185
2/2
✓ Branch 0 taken 64579599 times.
✓ Branch 1 taken 1445 times.
64581044 if (cs->ctype) {
1186 /*
1187 This will probably need a better implementation in the future:
1188 a function in CHARSET_INFO structure.
1189 */
1190
4/4
✓ Branch 0 taken 65022170 times.
✓ Branch 1 taken 332394 times.
✓ Branch 2 taken 774965 times.
✓ Branch 3 taken 64247205 times.
65354564 while (length && !my_isgraph(cs, *str)) { // Fix problem with yacc
1191 774965 length--;
1192 774965 str++;
1193 }
1194 }
1195
2/2
✓ Branch 0 taken 35683183 times.
✓ Branch 1 taken 28897922 times.
64581044 if (!my_charset_same(cs, system_charset_info)) {
1196 size_t res_length;
1197
1/2
✓ Branch 0 taken 35683183 times.
✗ Branch 1 not taken.
35683183 char *tmp = sql_strmake_with_convert(str, length, cs, MAX_ALIAS_NAME,
1198 system_charset_info, &res_length);
1199
1/2
✓ Branch 0 taken 35683185 times.
✗ Branch 1 not taken.
35683183 set(tmp, tmp ? res_length : 0);
1200 } else {
1201 28897922 size_t len = min<size_t>(length, MAX_ALIAS_NAME);
1202 28897922 char *tmp = sql_strmake(str, len);
1203
1/2
✓ Branch 0 taken 28897914 times.
✗ Branch 1 not taken.
28897914 set(tmp, tmp ? len : 0);
1204 }
1205 }
1206
1207 10767593 void Item_name_string::copy(const char *str_arg, size_t length_arg,
1208 const CHARSET_INFO *cs_arg,
1209 bool is_autogenerated_arg) {
1210 10767593 m_is_autogenerated = is_autogenerated_arg;
1211 10767593 copy(str_arg, length_arg, cs_arg);
1212
5/6
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 10767539 times.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 53 times.
✓ Branch 5 taken 10767539 times.
10767592 if (length_arg > length() && !is_autogenerated()) {
1213
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 ErrConvString tmp(str_arg, static_cast<uint>(length_arg), cs_arg);
1214
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 27 times.
53 if (length() == 0)
1215 26 push_warning_printf(
1216
2/4
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
26 current_thd, Sql_condition::SL_WARNING, ER_NAME_BECOMES_EMPTY,
1217
2/4
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
26 ER_THD(current_thd, ER_NAME_BECOMES_EMPTY), tmp.ptr());
1218 else
1219
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
54 push_warning_printf(current_thd, Sql_condition::SL_WARNING,
1220 ER_REMOVED_SPACES,
1221
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
27 ER_THD(current_thd, ER_REMOVED_SPACES), tmp.ptr());
1222 }
1223 10767592 }
1224
1225 /**
1226 @details
1227 This function is called when:
1228 - Comparing items in the WHERE clause (when doing where optimization)
1229 - When trying to find an ORDER BY/GROUP BY item in the SELECT part
1230 - When matching fields in multiple equality objects (Item_equal)
1231 */
1232
1233 11476 bool Item::eq(const Item *item, bool) const {
1234 /*
1235 Note, that this is never true if item is a Item_param:
1236 for all basic constants we have special checks, and Item_param's
1237 type() can be only among basic constant types.
1238 */
1239
4/4
✓ Branch 0 taken 8040 times.
✓ Branch 1 taken 3436 times.
✓ Branch 2 taken 161 times.
✓ Branch 3 taken 7879 times.
11476 return type() == item->type() && item_name.eq_safe(item->item_name);
1240 }
1241
1242 63508 Item *Item::safe_charset_converter(THD *thd, const CHARSET_INFO *tocs) {
1243 Item_func_conv_charset *conv =
1244
2/4
✓ Branch 0 taken 63508 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63508 times.
✗ Branch 3 not taken.
63508 new Item_func_conv_charset(thd, this, tocs, true);
1245
3/4
✓ Branch 0 taken 63508 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63396 times.
✓ Branch 3 taken 112 times.
63508 return conv && conv->m_safe ? conv : nullptr;
1246 }
1247
1248 /**
1249 @details
1250 Created mostly for mysql_prepare_table(). Important
1251 when a string ENUM/SET column is described with a numeric default value:
1252
1253 CREATE TABLE t1(a SET('a') DEFAULT 1);
1254
1255 We cannot use generic Item::safe_charset_converter(), because
1256 the latter returns a non-fixed Item, so val_str() crashes afterwards.
1257 Override Item_num method, to return a fixed item.
1258 */
1259 106 Item *Item_num::safe_charset_converter(THD *thd, const CHARSET_INFO *tocs) {
1260 /*
1261 Item_num returns pure ASCII result,
1262 so conversion is needed only in case of "tricky" character
1263 sets like UCS2. If tocs is not "tricky", return the item itself.
1264 */
1265
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 92 times.
106 if (!(tocs->state & MY_CS_NONASCII)) return this;
1266
1267 uint conv_errors;
1268 char buf[64], buf2[64];
1269 92 String tmp(buf, sizeof(buf), &my_charset_bin);
1270 92 String cstr(buf2, sizeof(buf2), &my_charset_bin);
1271
1/2
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
92 String *ostr = val_str(&tmp);
1272
1/2
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
92 cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
1273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
92 if (conv_errors > 0) {
1274 /*
1275 Safe conversion is not possible.
1276 We could not convert a string into the requested character set
1277 without data loss. The target charset does not cover all the
1278 characters from the string. Operation cannot be done correctly.
1279 */
1280 return nullptr;
1281 }
1282
1283
1/2
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
92 char *ptr = thd->strmake(cstr.ptr(), cstr.length());
1284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
92 if (ptr == nullptr) return nullptr;
1285 auto conv =
1286
2/4
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
✗ Branch 3 not taken.
92 new Item_string(ptr, cstr.length(), cstr.charset(), collation.derivation);
1287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
92 if (conv == nullptr) return nullptr;
1288
1289 /* Ensure that no one is going to change the result string */
1290 92 conv->mark_result_as_const();
1291
1/2
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
92 conv->fix_char_length(max_char_length());
1292 92 return conv;
1293 92 }
1294
1295 Item *Item_func_pi::safe_charset_converter(THD *thd, const CHARSET_INFO *) {
1296 char buf[64];
1297 String tmp(buf, sizeof(buf), &my_charset_bin);
1298 String *s = val_str(&tmp);
1299 char *ptr = thd->strmake(s->ptr(), s->length());
1300 if (ptr == nullptr) return nullptr;
1301 auto conv =
1302 new Item_static_string_func(func_name, ptr, s->length(), s->charset());
1303 if (conv == nullptr) return nullptr;
1304 conv->mark_result_as_const();
1305 return conv;
1306 }
1307
1308 1500753 Item *Item_string::safe_charset_converter(THD *thd, const CHARSET_INFO *tocs) {
1309 1500753 return charset_converter(thd, tocs, true);
1310 }
1311
1312 /**
1313 Convert a string item into the requested character set.
1314
1315 @param thd Thread handle.
1316 @param tocs Character set to to convert the string to.
1317 @param lossless Whether data loss is acceptable.
1318
1319 @return A new item representing the converted string.
1320 */
1321 1501085 Item *Item_string::charset_converter(THD *thd, const CHARSET_INFO *tocs,
1322 bool lossless) {
1323 uint conv_errors;
1324
1/2
✓ Branch 0 taken 1501085 times.
✗ Branch 1 not taken.
1501085 String tmp, cstr, *ostr = val_str(&tmp);
1325
1/2
✓ Branch 0 taken 1501085 times.
✗ Branch 1 not taken.
1501085 cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
1326
4/4
✓ Branch 0 taken 1500753 times.
✓ Branch 1 taken 332 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 1500742 times.
1501085 if (lossless && conv_errors > 0) {
1327 /*
1328 Safe conversion is not possible.
1329 We could not convert a string into the requested character set
1330 without data loss. The target charset does not cover all the
1331 characters from the string. Operation cannot be done correctly.
1332 */
1333 11 return nullptr;
1334 }
1335
1336
1/2
✓ Branch 0 taken 1501074 times.
✗ Branch 1 not taken.
1501074 char *ptr = thd->strmake(cstr.ptr(), cstr.length());
1337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1501074 times.
1501074 if (ptr == nullptr) return nullptr;
1338 auto conv =
1339
2/4
✓ Branch 0 taken 1501074 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1501074 times.
✗ Branch 3 not taken.
1501074 new Item_string(ptr, cstr.length(), cstr.charset(), collation.derivation);
1340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1501074 times.
1501074 if (conv == nullptr) return nullptr;
1341 /* Ensure that no one is going to change the result string */
1342 1501074 conv->mark_result_as_const();
1343 1501074 return conv;
1344 1501085 }
1345
1346 Item *Item_param::safe_charset_converter(THD *thd, const CHARSET_INFO *tocs) {
1347 if (may_evaluate_const(thd)) {
1348 String tmp, cstr, *ostr = val_str(&tmp);
1349
1350 if (null_value) {
1351 auto cnvitem = new Item_null();
1352 if (cnvitem == nullptr) return nullptr;
1353 cnvitem->collation.set(tocs);
1354 return cnvitem;
1355 } else {
1356 uint conv_errors;
1357 cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs,
1358 &conv_errors);
1359
1360 if (conv_errors > 0) return nullptr;
1361
1362 char *ptr = thd->strmake(cstr.ptr(), cstr.length());
1363 if (ptr == nullptr) return nullptr;
1364 auto cnvitem = new Item_string(ptr, cstr.length(), cstr.charset(),
1365 collation.derivation);
1366 if (cnvitem == nullptr) return nullptr;
1367 cnvitem->mark_result_as_const();
1368 return cnvitem;
1369 }
1370 }
1371 return Item::safe_charset_converter(thd, tocs);
1372 }
1373
1374 4 Item *Item_static_string_func::safe_charset_converter(
1375 THD *thd, const CHARSET_INFO *tocs) {
1376 uint conv_errors;
1377
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 String tmp, cstr, *ostr = val_str(&tmp);
1378
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
1379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (conv_errors > 0) {
1380 /*
1381 Safe conversion is not possible.
1382 We could not convert a string into the requested character set
1383 without data loss. The target charset does not cover all the
1384 characters from the string. Operation cannot be done correctly.
1385 */
1386 return nullptr;
1387 }
1388
1389
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 char *ptr = thd->strmake(cstr.ptr(), cstr.length());
1390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ptr == nullptr) return nullptr;
1391 4 auto conv = new Item_static_string_func(func_name, ptr, cstr.length(),
1392
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 cstr.charset(), collation.derivation);
1393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (conv == nullptr) return nullptr;
1394 /* Ensure that no one is going to change the result string */
1395 4 conv->mark_result_as_const();
1396 4 return conv;
1397 4 }
1398
1399 1013244 bool Item_string::eq(const Item *item, bool binary_cmp) const {
1400
6/6
✓ Branch 0 taken 530718 times.
✓ Branch 1 taken 482604 times.
✓ Branch 2 taken 530696 times.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 530703 times.
✓ Branch 5 taken 482619 times.
1013244 if (type() == item->type() && item->basic_const_item()) {
1401 // Should be OK for a basic constant.
1402 530703 Item *arg = const_cast<Item *>(item);
1403 530703 String str;
1404
4/6
✓ Branch 0 taken 487565 times.
✓ Branch 1 taken 43138 times.
✓ Branch 2 taken 487574 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 487564 times.
✗ Branch 5 not taken.
530703 if (binary_cmp) return !stringcmp(&str_value, arg->val_str(&str));
1405
2/2
✓ Branch 0 taken 29297 times.
✓ Branch 1 taken 13841 times.
72435 return (collation.collation == arg->collation.collation &&
1406
4/6
✓ Branch 0 taken 29297 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29297 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28180 times.
✓ Branch 5 taken 1117 times.
72435 !sortcmp(&str_value, arg->val_str(&str), collation.collation));
1407 530702 }
1408 482619 return false;
1409 }
1410
1411 10770 bool Item::get_date_from_string(MYSQL_TIME *ltime, my_time_flags_t flags) {
1412 char buff[MAX_DATE_STRING_REP_LENGTH];
1413 10770 String tmp(buff, sizeof(buff), &my_charset_bin), *res;
1414
3/4
✓ Branch 0 taken 10770 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 10734 times.
10770 if (!(res = val_str(&tmp))) {
1415
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 set_zero_time(ltime, MYSQL_TIMESTAMP_DATETIME);
1416 36 return true;
1417 }
1418
1/2
✓ Branch 0 taken 10734 times.
✗ Branch 1 not taken.
10734 return str_to_datetime_with_warn(res, ltime, flags);
1419 10770 }
1420
1421 10 bool Item::get_date_from_real(MYSQL_TIME *ltime, my_time_flags_t flags) {
1422 10 double value = val_real();
1423
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
10 if (null_value) {
1424 1 set_zero_time(ltime, MYSQL_TIMESTAMP_DATETIME);
1425 1 return true;
1426 }
1427 9 return my_double_to_datetime_with_warn(value, ltime, flags);
1428 }
1429
1430 60 bool Item::get_date_from_decimal(MYSQL_TIME *ltime, my_time_flags_t flags) {
1431
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 my_decimal buf, *decimal = val_decimal(&buf);
1432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (null_value) {
1433 set_zero_time(ltime, MYSQL_TIMESTAMP_DATETIME);
1434 return true;
1435 }
1436
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 return my_decimal_to_datetime_with_warn(decimal, ltime, flags);
1437 60 }
1438
1439 617 bool Item::get_date_from_int(MYSQL_TIME *ltime, my_time_flags_t flags) {
1440 617 longlong value = val_int();
1441
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 613 times.
617 if (null_value) {
1442 4 set_zero_time(ltime, MYSQL_TIMESTAMP_DATETIME);
1443 4 return true;
1444 }
1445 613 return my_longlong_to_datetime_with_warn(value, ltime, flags);
1446 }
1447
1448 161 bool Item::get_date_from_time(MYSQL_TIME *ltime) {
1449 MYSQL_TIME tm;
1450
3/4
✓ Branch 0 taken 161 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 150 times.
161 if (get_time(&tm)) {
1451
5/8
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
11 assert(null_value || current_thd->is_error());
1452 11 return true;
1453 }
1454
2/4
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 150 times.
✗ Branch 3 not taken.
150 time_to_datetime(current_thd, &tm, ltime);
1455 150 return false;
1456 }
1457
1458 3 bool Item::get_date_from_numeric(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) {
1459
3/5
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
3 switch (result_type()) {
1460 1 case REAL_RESULT:
1461 1 return get_date_from_real(ltime, fuzzydate);
1462 1 case DECIMAL_RESULT:
1463 1 return get_date_from_decimal(ltime, fuzzydate);
1464 1 case INT_RESULT:
1465 1 return get_date_from_int(ltime, fuzzydate);
1466 case STRING_RESULT:
1467 case ROW_RESULT:
1468 case INVALID_RESULT:
1469 assert(0);
1470 }
1471 return (null_value = true); // Impossible result_type
1472 }
1473
1474 /**
1475 Get the value of the function as a MYSQL_TIME structure.
1476 As a extra convenience the time structure is reset on error!
1477 */
1478
1479 261 bool Item::get_date_from_non_temporal(MYSQL_TIME *ltime,
1480 my_time_flags_t fuzzydate) {
1481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261 times.
261 assert(!is_temporal());
1482
4/6
✓ Branch 0 taken 223 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 17 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
261 switch (result_type()) {
1483 223 case STRING_RESULT:
1484 223 return get_date_from_string(ltime, fuzzydate);
1485 1 case REAL_RESULT:
1486 1 return get_date_from_real(ltime, fuzzydate);
1487 20 case DECIMAL_RESULT:
1488 20 return get_date_from_decimal(ltime, fuzzydate);
1489 17 case INT_RESULT:
1490 17 return get_date_from_int(ltime, fuzzydate);
1491 case ROW_RESULT:
1492 case INVALID_RESULT:
1493 assert(0);
1494 }
1495 return (null_value = true); // Impossible result_type
1496 }
1497
1498 2977 bool Item::get_time_from_string(MYSQL_TIME *ltime) {
1499 char buff[MAX_DATE_STRING_REP_LENGTH];
1500 2977 String tmp(buff, sizeof(buff), &my_charset_bin), *res;
1501
3/4
✓ Branch 0 taken 2977 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 2967 times.
2977 if (!(res = val_str(&tmp))) {
1502
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
1503 10 return true;
1504 }
1505
1/2
✓ Branch 0 taken 2967 times.
✗ Branch 1 not taken.
2967 return str_to_time_with_warn(res, ltime);
1506 2977 }
1507
1508 18 bool Item::get_time_from_real(MYSQL_TIME *ltime) {
1509 18 double value = val_real();
1510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (null_value) {
1511 set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
1512 return true;
1513 }
1514 18 return my_double_to_time_with_warn(value, ltime);
1515 }
1516
1517 79 bool Item::get_time_from_decimal(MYSQL_TIME *ltime) {
1518
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 my_decimal buf, *decimal = val_decimal(&buf);
1519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 if (null_value) {
1520 set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
1521 return true;
1522 }
1523
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 return my_decimal_to_time_with_warn(decimal, ltime);
1524 79 }
1525
1526 450 bool Item::get_time_from_int(MYSQL_TIME *ltime) {
1527 450 longlong value = val_int();
1528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
450 if (null_value) {
1529 set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
1530 return true;
1531 }
1532 450 return my_longlong_to_time_with_warn(value, ltime);
1533 }
1534
1535 11 bool Item::get_time_from_date(MYSQL_TIME *ltime) {
1536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 assert(fixed == 1);
1537
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7 times.
11 if (get_date(ltime, TIME_FUZZY_DATE)) // Need this check if NULL value
1538 4 return true;
1539 7 set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
1540 7 return false;
1541 }
1542
1543 53 bool Item::get_time_from_datetime(MYSQL_TIME *ltime) {
1544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 assert(fixed == 1);
1545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
53 if (get_date(ltime, TIME_FUZZY_DATE)) return true;
1546 53 datetime_to_time(ltime);
1547 53 return false;
1548 }
1549
1550 6 bool Item::get_time_from_numeric(MYSQL_TIME *ltime) {
1551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 assert(!is_temporal());
1552
3/5
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6 switch (result_type()) {
1553 1 case REAL_RESULT:
1554 1 return get_time_from_real(ltime);
1555 1 case DECIMAL_RESULT:
1556 1 return get_time_from_decimal(ltime);
1557 4 case INT_RESULT:
1558 4 return get_time_from_int(ltime);
1559 case STRING_RESULT:
1560 case ROW_RESULT:
1561 case INVALID_RESULT:
1562 assert(0);
1563 }
1564 return (null_value = true); // Impossible result type
1565 }
1566
1567 /**
1568 Get time value from int, real, decimal or string.
1569
1570 As a extra convenience the time structure is reset on error!
1571 */
1572
1573 65 bool Item::get_time_from_non_temporal(MYSQL_TIME *ltime) {
1574
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 assert(!is_temporal());
1575
3/6
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
65 switch (result_type()) {
1576 60 case STRING_RESULT:
1577 60 return get_time_from_string(ltime);
1578 3 case REAL_RESULT:
1579 3 return get_time_from_real(ltime);
1580 case DECIMAL_RESULT:
1581 return get_time_from_decimal(ltime);
1582 2 case INT_RESULT:
1583 2 return get_time_from_int(ltime);
1584 case ROW_RESULT:
1585 case INVALID_RESULT:
1586 assert(0);
1587 }
1588 return (null_value = true); // Impossible result type
1589 }
1590
1591 /**
1592 If argument is NULL, sets null_value. Otherwise:
1593 if invalid DATETIME value, or a valid DATETIME value but which is out of
1594 the supported Unix timestamp range, sets 'tm' to 0.
1595 */
1596 428 bool Item::get_timeval(my_timeval *tm, int *warnings) {
1597 MYSQL_TIME ltime;
1598
3/4
✓ Branch 0 taken 428 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 415 times.
428 if (get_date(&ltime, TIME_FUZZY_DATE)) {
1599
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 4 times.
13 if (null_value) return true; /* Value is NULL */
1600 4 goto zero; /* Could not extract date from the value */
1601 }
1602
4/6
✓ Branch 0 taken 415 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 415 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 391 times.
415 if (datetime_to_timeval(&ltime, *current_thd->time_zone(), tm, warnings))
1603 24 goto zero; /* Value is out of the supported range */
1604 391 return false; /* Value is a good Unix timestamp */
1605 28 zero:
1606 28 tm->m_tv_sec = tm->m_tv_usec = 0;
1607 28 return false;
1608 }
1609
1610 6775492 const CHARSET_INFO *Item::default_charset() {
1611 6775492 return current_thd->variables.collation_connection;
1612 }
1613
1614 /*
1615 Save value in field, but don't give any warnings
1616
1617 NOTES
1618 This is used to temporary store and retrieve a value in a column,
1619 for example in opt_range to adjust the key value to fit the column.
1620 */
1621
1622 11675132 type_conversion_status Item::save_in_field_no_warnings(Field *field,
1623 bool no_conversions) {
1624
1/2
✓ Branch 0 taken 11675446 times.
✗ Branch 1 not taken.
11675132 DBUG_TRACE;
1625 11675446 TABLE *table = field->table;
1626
1/2
✓ Branch 0 taken 11675440 times.
✗ Branch 1 not taken.
11675446 THD *thd = current_thd;
1627 11675440 enum_check_fields tmp = thd->check_for_truncated_fields;
1628 11675440 my_bitmap_map *old_map = dbug_tmp_use_all_columns(table, table->write_set);
1629 11675363 sql_mode_t sql_mode = thd->variables.sql_mode;
1630 /*
1631 For cases like data truncation still warning is reported here. Which was
1632 avoided before with THD::abort_on_warning flag. Since the flag is removed
1633 now, until MODE_NO_ZERO_IN_DATE, MODE_NO_ZERO_DATE and
1634 MODE_ERROR_FOR_DIVISION_BY_ZERO are merged with strict mode, removing even
1635 strict modes from sql_mode here to avoid warnings.
1636 */
1637 11675363 thd->variables.sql_mode &=
1638 ~(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE | MODE_STRICT_ALL_TABLES |
1639 MODE_STRICT_TRANS_TABLES);
1640 11675363 thd->check_for_truncated_fields = CHECK_FIELD_IGNORE;
1641
1642
1/2
✓ Branch 0 taken 11675468 times.
✗ Branch 1 not taken.
11675363 const type_conversion_status res = save_in_field(field, no_conversions);
1643
1644 11675468 thd->check_for_truncated_fields = tmp;
1645 11675468 dbug_tmp_restore_column_map(table->write_set, old_map);
1646 11675422 thd->variables.sql_mode = sql_mode;
1647 11675473 return res;
1648 11675422 }
1649
1650 13143 bool Item::is_blob_field() const {
1651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13143 times.
13143 assert(fixed);
1652
1653 13143 enum_field_types type = data_type();
1654
4/4
✓ Branch 0 taken 12820 times.
✓ Branch 1 taken 323 times.
✓ Branch 2 taken 12803 times.
✓ Branch 3 taken 17 times.
25946 return (type == MYSQL_TYPE_BLOB || type == MYSQL_TYPE_GEOMETRY ||
1655 // Char length, not the byte one, should be taken into account
1656
2/2
✓ Branch 0 taken 152 times.
✓ Branch 1 taken 12651 times.
12803 max_length / collation.collation->mbmaxlen >
1657 13143 CONVERT_IF_BIGGER_TO_BLOB);
1658 }
1659
1660 /*****************************************************************************
1661 Item_sp_variable methods
1662 *****************************************************************************/
1663
1664 647962 Item_sp_variable::Item_sp_variable(const Name_string sp_var_name)
1665 647962 : m_name(sp_var_name) {}
1666
1667 97556 bool Item_sp_variable::fix_fields(THD *, Item **) {
1668 97556 Item *it = this_item();
1669
1670
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 97561 times.
97561 assert(it->fixed);
1671
1672 97561 max_length = it->max_length;
1673 97561 decimals = it->decimals;
1674 97561 unsigned_flag = it->unsigned_flag;
1675 97561 collation.set(it->collation);
1676 97560 set_data_type(it->data_type());
1677
1678 97559 fixed = true;
1679
1680 97559 return false;
1681 }
1682
1683 228581 double Item_sp_variable::val_real() {
1684
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 228581 times.
228581 assert(fixed);
1685 228581 Item *it = this_item();
1686 228584 double ret = it->val_real();
1687 228584 null_value = it->null_value;
1688 228584 return ret;
1689 }
1690
1691 8364409 longlong Item_sp_variable::val_int() {
1692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8364409 times.
8364409 assert(fixed);
1693 8364409 Item *it = this_item();
1694 8365431 longlong ret = it->val_int();
1695 8365822 null_value = it->null_value;
1696 8365822 return ret;
1697 }
1698
1699 1880750 String *Item_sp_variable::val_str(String *sp) {
1700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1880750 times.
1880750 assert(fixed);
1701 1880750 Item *it = this_item();
1702 1880815 String *res = it->val_str(sp);
1703
1704 1880794 null_value = it->null_value;
1705
1706
2/2
✓ Branch 0 taken 215 times.
✓ Branch 1 taken 1880579 times.
1880794 if (!res) return nullptr;
1707
1708 /*
1709 This way we mark returned value of val_str as const,
1710 so that various functions (e.g. CONCAT) won't try to
1711 modify the value of the Item. Analogous mechanism is
1712 implemented for Item_param.
1713 Without this trick Item_splocal could be changed as a
1714 side-effect of expression computation. Here is an example
1715 of what happens without it: suppose x is varchar local
1716 variable in a SP with initial value 'ab' Then
1717 select concat(x,'c');
1718 would change x's value to 'abc', as Item_func_concat::val_str()
1719 would use x's internal buffer to compute the result.
1720 This is intended behaviour of Item_func_concat. Comments to
1721 Item_param class contain some more details on the topic.
1722 */
1723
1724
1/2
✓ Branch 0 taken 1880579 times.
✗ Branch 1 not taken.
1880579 if (res != &str_value)
1725 1880579 str_value.set(res->ptr(), res->length(), res->charset());
1726 else
1727 res->mark_as_const();
1728
1729 1880604 return &str_value;
1730 }
1731
1732 463815 my_decimal *Item_sp_variable::val_decimal(my_decimal *decimal_value) {
1733
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 463815 times.
463815 assert(fixed);
1734 463815 Item *it = this_item();
1735 463819 my_decimal *val = it->val_decimal(decimal_value);
1736 463817 null_value = it->null_value;
1737 463817 return val;
1738 }
1739
1740 bool Item_sp_variable::val_json(Json_wrapper *wr) {
1741 assert(fixed);
1742 Item *it = this_item();
1743 bool result = it->val_json(wr);
1744 null_value = it->null_value;
1745 return result;
1746 }
1747
1748 27 bool Item_sp_variable::get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) {
1749
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 assert(fixed);
1750 27 Item *it = this_item();
1751 27 return (null_value = it->get_date(ltime, fuzzydate));
1752 }
1753
1754 5 bool Item_sp_variable::get_time(MYSQL_TIME *ltime) {
1755
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 assert(fixed);
1756 5 Item *it = this_item();
1757 5 return (null_value = it->get_time(ltime));
1758 }
1759
1760 195928 bool Item_sp_variable::is_null() { return this_item()->is_null(); }
1761
1762 /*****************************************************************************
1763 Item_splocal methods
1764 *****************************************************************************/
1765
1766 626248 Item_splocal::Item_splocal(const Name_string sp_var_name, uint sp_var_idx,
1767 enum_field_types sp_var_type, uint pos_in_q,
1768 626248 uint len_in_q)
1769 : Item_sp_variable(sp_var_name),
1770 626249 m_var_idx(sp_var_idx),
1771 626249 limit_clause_param(false),
1772 626249 pos_in_query(pos_in_q),
1773 626248 len_in_query(len_in_q) {
1774 626249 set_nullable(true);
1775
1776 626249 sp_var_type = real_type_to_type(sp_var_type);
1777
1/2
✓ Branch 0 taken 626247 times.
✗ Branch 1 not taken.
626247 m_type = sp_map_item_type(sp_var_type);
1778 626247 set_data_type(sp_var_type);
1779
1/2
✓ Branch 0 taken 626247 times.
✗ Branch 1 not taken.
626246 m_result_type = sp_map_result_type(sp_var_type);
1780 626247 }
1781
1782 15251827 Item *Item_splocal::this_item() {
1783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15252615 times.
15251827 assert(m_sp == current_thd->sp_runtime_ctx->sp);
1784
1785 15252615 return current_thd->sp_runtime_ctx->get_item(m_var_idx);
1786 }
1787
1788 const Item *Item_splocal::this_item() const {
1789 assert(m_sp == current_thd->sp_runtime_ctx->sp);
1790
1791 return current_thd->sp_runtime_ctx->get_item(m_var_idx);
1792 }
1793
1794 216384 Item **Item_splocal::this_item_addr(THD *thd, Item **) {
1795
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216384 times.
216384 assert(m_sp == thd->sp_runtime_ctx->sp);
1796
1797 216384 return thd->sp_runtime_ctx->get_item_addr(m_var_idx);
1798 }
1799
1800 103 bool Item_splocal::val_json(Json_wrapper *result) {
1801 103 Item *it = this_item();
1802 103 bool ret = it->val_json(result);
1803 103 null_value = it->null_value;
1804 103 return ret;
1805 }
1806
1807 7351 void Item_splocal::print(const THD *thd, String *str, enum_query_type) const {
1808 // While reparsing a derived table condition, print the SP variable name.
1809 // Otherwise, print the SP variable name, followed by '@' and the variable
1810 // index.
1811 7351 str->reserve(m_name.length() + 8);
1812 7351 str->append(m_name);
1813
2/2
✓ Branch 0 taken 7335 times.
✓ Branch 1 taken 16 times.
7351 if (!thd->lex->reparse_derived_table_condition) {
1814 7335 str->append('@');
1815 7335 qs_append(m_var_idx, str);
1816 }
1817 7351 }
1818
1819 761 bool Item_splocal::set_value(THD *thd, sp_rcontext *ctx, Item **it) {
1820 761 return ctx->set_variable(thd, get_var_idx(), it);
1821 }
1822
1823 /*****************************************************************************
1824 Item_case_expr methods
1825 *****************************************************************************/
1826
1827 21714 Item_case_expr::Item_case_expr(uint case_expr_id)
1828 : Item_sp_variable(Name_string(STRING_WITH_LEN("case_expr"))),
1829
1/2
✓ Branch 0 taken 21714 times.
✗ Branch 1 not taken.
21714 m_case_expr_id(case_expr_id) {}
1830
1831 142452 Item *Item_case_expr::this_item() {
1832
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 142455 times.
142452 assert(m_sp == current_thd->sp_runtime_ctx->sp);
1833
1834 142455 return current_thd->sp_runtime_ctx->get_case_expr(m_case_expr_id);
1835 }
1836
1837 86656 const Item *Item_case_expr::this_item() const {
1838
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86656 times.
86656 assert(m_sp == current_thd->sp_runtime_ctx->sp);
1839
1840 86656 return current_thd->sp_runtime_ctx->get_case_expr(m_case_expr_id);
1841 }
1842
1843 Item **Item_case_expr::this_item_addr(THD *thd, Item **) {
1844 assert(m_sp == thd->sp_runtime_ctx->sp);
1845
1846 return thd->sp_runtime_ctx->get_case_expr_addr(m_case_expr_id);
1847 }
1848
1849 27 void Item_case_expr::print(const THD *, String *str, enum_query_type) const {
1850
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (str->reserve(MAX_INT_WIDTH + sizeof("case_expr@")))
1851 return; /* purecov: inspected */
1852 27 (void)str->append(STRING_WITH_LEN("case_expr@"));
1853 27 qs_append(m_case_expr_id, str);
1854 }
1855
1856 /*****************************************************************************
1857 Item_name_const methods
1858 *****************************************************************************/
1859
1860 8 double Item_name_const::val_real() {
1861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 assert(fixed);
1862 8 double ret = value_item->val_real();
1863 8 null_value = value_item->null_value;
1864 8 return ret;
1865 }
1866
1867 736 longlong Item_name_const::val_int() {
1868
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 736 times.
736 assert(fixed);
1869 736 longlong ret = value_item->val_int();
1870 736 null_value = value_item->null_value;
1871 736 return ret;
1872 }
1873
1874 15 String *Item_name_const::val_str(String *sp) {
1875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 assert(fixed);
1876 15 String *ret = value_item->val_str(sp);
1877 15 null_value = value_item->null_value;
1878 15 return ret;
1879 }
1880
1881 2 my_decimal *Item_name_const::val_decimal(my_decimal *decimal_value) {
1882
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 assert(fixed);
1883 2 my_decimal *val = value_item->val_decimal(decimal_value);
1884 2 null_value = value_item->null_value;
1885 2 return val;
1886 }
1887
1888 1 bool Item_name_const::get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) {
1889
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(fixed);
1890 1 return (null_value = value_item->get_date(ltime, fuzzydate));
1891 }
1892
1893 1 bool Item_name_const::get_time(MYSQL_TIME *ltime) {
1894
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(fixed);
1895 1 return (null_value = value_item->get_time(ltime));
1896 }
1897
1898 41 bool Item_name_const::is_null() { return value_item->is_null(); }
1899
1900 8674 Item_name_const::Item_name_const(const POS &pos, Item *name_arg, Item *val)
1901 8674 : super(pos), value_item(val), name_item(name_arg) {
1902 8675 set_nullable(true);
1903 8674 }
1904
1905 8674 bool Item_name_const::itemize(Parse_context *pc, Item **res) {
1906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8675 times.
8674 if (skip_itemize(res)) return false;
1907
3/6
✓ Branch 0 taken 8675 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8674 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 8675 times.
17350 if (super::itemize(pc, res) || value_item->itemize(pc, &value_item) ||
1908
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8675 times.
8674 name_item->itemize(pc, &name_item))
1909 return true;
1910 /*
1911 The name and value argument to NAME_CONST can only be a literal constant.
1912 This (internal, although documented) feature is only supported for the
1913 stored procedure binlog's needs, cf. subst_spvars().
1914
1915 Apart from plain literals, some extra logic are needed to support a
1916 collation specifier and to handle negative constant values.
1917 */
1918 8675 valid_args = false;
1919
1920
2/2
✓ Branch 0 taken 8672 times.
✓ Branch 1 taken 1 times.
8675 if (name_item->basic_const_item()) {
1921
1/2
✓ Branch 0 taken 8672 times.
✗ Branch 1 not taken.
8672 Item_func *func = dynamic_cast<Item_func *>(value_item);
1922 8672 Item *possible_const = value_item;
1923
1924
6/6
✓ Branch 0 taken 2135 times.
✓ Branch 1 taken 6537 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 2123 times.
✓ Branch 4 taken 2132 times.
✓ Branch 5 taken 6539 times.
8683 if (func && (func->functype() == Item_func::COLLATE_FUNC ||
1925
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2 times.
12 func->functype() == Item_func::NEG_FUNC)) {
1926 /*
1927 The value is not a literal constant. Accept it if it's a
1928 COLLATE_FUNC or a NEG_FUNC wrapping a literal constant.
1929 */
1930 2132 possible_const = func->key_item();
1931 }
1932
1933 /*
1934 There should now be no constant items which are functions left,
1935 (e.g. like TIME '1'), since none such are generated by subst_spvars() and
1936 sp_get_item_value(), which is where NAME_CONST calls are generated
1937 internally for the binary log: hence the second predicate below. If user
1938 applications try to use such constructs, or any non-constant contents for
1939 NAME_CONST's value argument (#2), we generate an error.
1940 */
1941
2/2
✓ Branch 0 taken 8664 times.
✓ Branch 1 taken 7 times.
17335 valid_args = (possible_const->basic_const_item() &&
1942
1/2
✓ Branch 0 taken 8664 times.
✗ Branch 1 not taken.
8664 possible_const->type() != FUNC_ITEM);
1943 }
1944
1945
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 8662 times.
8672 if (!valid_args) {
1946 10 my_error(ER_WRONG_ARGUMENTS, MYF(0), "NAME_CONST");
1947 10 return true;
1948 }
1949
1950 8662 return false;
1951 }
1952
1953 8340 Item::Type Item_name_const::type() const {
1954 /*
1955 As
1956 1. one can try to create the Item_name_const passing non-constant
1957 arguments, although it's incorrect and
1958 2. the type() method can be called before the fix_fields() to get
1959 type information for a further type cast, e.g.
1960 if (item->type() == FIELD_ITEM)
1961 ((Item_field *) item)->...
1962 we return NULL_ITEM in the case to avoid wrong casting.
1963
1964 valid_args guarantees value_item->basic_const_item(); if type is
1965 FUNC_ITEM, then we have a fudged item_func_neg() on our hands
1966 and return the underlying type.
1967 For Item_func_set_collation()
1968 e.g. NAME_CONST('name', 'value' COLLATE collation) we return its
1969 'value' argument type.
1970 */
1971
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8340 times.
8340 if (!valid_args) return NULL_ITEM;
1972 8340 Item::Type value_type = value_item->type();
1973
2/2
✓ Branch 0 taken 2235 times.
✓ Branch 1 taken 6106 times.
8341 if (value_type == FUNC_ITEM) {
1974 /*
1975 The second argument of NAME_CONST('name', 'value') must be
1976 a simple constant item or a NEG_FUNC/COLLATE_FUNC.
1977 */
1978 2235 Item_func *func = down_cast<Item_func *>(value_item);
1979
3/4
✓ Branch 0 taken 2214 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2214 times.
2235 assert(func->functype() == Item_func::NEG_FUNC ||
1980 func->functype() == Item_func::COLLATE_FUNC);
1981 2235 return func->key_item()->type();
1982 }
1983 6106 return value_type;
1984 }
1985
1986 8644 bool Item_name_const::fix_fields(THD *thd, Item **) {
1987 char buf[128];
1988 String *tmp;
1989 8644 String s(buf, sizeof(buf), &my_charset_bin);
1990 8644 s.length(0);
1991
1992
1/2
✓ Branch 0 taken 8646 times.
✗ Branch 1 not taken.
8644 if (value_item->fix_fields(thd, &value_item) ||
1993
4/8
✓ Branch 0 taken 8645 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8645 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8646 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8646 times.
✗ Branch 7 not taken.
8646 name_item->fix_fields(thd, &name_item) || !value_item->const_item() ||
1994
4/8
✓ Branch 0 taken 8646 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8646 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8646 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 8645 times.
25937 !name_item->const_item() ||
1995
2/4
✓ Branch 0 taken 8645 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8645 times.
8646 !(tmp = name_item->val_str(&s))) // Can't have a NULL name
1996 {
1997 my_error(ER_RESERVED_SYNTAX, MYF(0), "NAME_CONST");
1998 return true;
1999 }
2000
2/2
✓ Branch 0 taken 8643 times.
✓ Branch 1 taken 3 times.
8645 if (item_name.is_autogenerated()) {
2001
1/2
✓ Branch 0 taken 8643 times.
✗ Branch 1 not taken.
8643 item_name.copy(tmp->ptr(), (uint)tmp->length(), system_charset_info);
2002 }
2003 8646 collation.set(value_item->collation.collation,
2004 8646 value_item->collation.derivation,
2005 8646 value_item->collation.repertoire);
2006 8646 set_data_type(value_item->data_type());
2007 8646 max_length = value_item->max_length;
2008 8646 decimals = value_item->decimals;
2009 8646 fixed = true;
2010 8646 return false;
2011 8646 }
2012
2013 void Item_name_const::print(const THD *thd, String *str,
2014 enum_query_type query_type) const {
2015 str->append(STRING_WITH_LEN("NAME_CONST("));
2016 name_item->print(thd, str, query_type);
2017 str->append(',');
2018 value_item->print(thd, str, query_type);
2019 str->append(')');
2020 }
2021
2022 /*
2023 need a special class to adjust printing : references to aggregate functions
2024 must not be printed as refs because the aggregate functions that are added to
2025 the front of select list are not printed as well.
2026 */
2027 class Item_aggregate_ref : public Item_ref {
2028 public:
2029 334567 Item_aggregate_ref(Name_resolution_context *context_arg, Item **item,
2030 const char *db_name_arg, const char *table_name_arg,
2031 const char *field_name_arg, Query_block *depended_from_arg)
2032 334567 : Item_ref(context_arg, item, db_name_arg, table_name_arg,
2033 334567 field_name_arg) {
2034 334567 depended_from = depended_from_arg;
2035 334567 }
2036
2037 142178 void print(const THD *thd, String *str,
2038 enum_query_type query_type) const override {
2039
1/2
✓ Branch 0 taken 142178 times.
✗ Branch 1 not taken.
142178 if (ref)
2040 142178 (*ref)->print(thd, str, query_type);
2041 else
2042 Item_ident::print(thd, str, query_type);
2043 142178 }
2044 206886 Ref_Type ref_type() const override { return AGGREGATE_REF; }
2045
2046 /**
2047 Walker processor used by Query_block::transform_grouped_to_derived to
2048 replace an aggregate's reference to one in the new derived table's (hidden)
2049 select list.
2050
2051 @param arg An info object of type Item::Aggregate_ref_update
2052 @returns false
2053 */
2054 bool update_aggr_refs(uchar *arg) override {
2055 auto *info = pointer_cast<Item::Aggregate_ref_update *>(arg);
2056 if (*ref != info->m_target) return false;
2057 ref = info->m_owner->add_hidden_item(info->m_target);
2058 return false;
2059 }
2060 };
2061
2062 /**
2063 1. Move SUM items out from item tree and replace with reference.
2064
2065 The general goal of this is to get a list of group aggregates, and window
2066 functions, and their arguments, so that the code which manages internal tmp
2067 tables (creation, row copying) has a list of all aggregates (which require
2068 special management) and a list of their arguments (which must be carried
2069 from tmp table to tmp table until the aggregate can be computed).
2070
2071 2. Move scalar subqueries out of the item tree and replace with reference
2072 when used in arguments to window functions for similar reasons (tmp tables).
2073
2074 @param thd Current session
2075 @param ref_item_array Pointer to array of reference fields
2076 @param fields All fields in select
2077 @param ref Pointer to item. If nullptr, get it from
2078 Item_sum::referenced_by[].
2079 @param skip_registered <=> function be must skipped for registered SUM items
2080
2081 All found SUM items are added FIRST in the fields list and
2082 we replace the item with a reference.
2083
2084 thd->fatal_error() may be called if we are out of memory
2085
2086 The logic of skip_registered is:
2087
2088 - split_sum_func() is called when an aggregate is part of a bigger
2089 expression, example: '1+max()'.
2090
2091 - an Item_sum has referenced_by[0]!=nullptr when it is a group aggregate
2092 located in a subquery but aggregating in a more outer query.
2093
2094 - this referenced_by is necessary because for such aggregates, there are
2095 two phases:
2096
2097 - fix_fields() is called by the subquery, which puts the item into the
2098 outer Query_block::inner_sum_func_list.
2099
2100 - the outer query scans that list, calls split_sum_func2(), it
2101 replaces the aggregate with an Item_ref, so it needs to correct the
2102 pointer-to-aggregate held by the '+' item; so it needs access to the
2103 pointer; this is possible because fix_fields() has stored the
2104 address of this pointer into referenced_by[0].
2105
2106 - So when we call split_sum_func for any aggregate, if we are in the
2107 subquery, we do not want to modify the outer-aggregated aggregates, and
2108 as those are detectable because they have referenced_by[0]!=0: we pass
2109 'skip_registered=true'.
2110
2111 - On the other hand, if we are in the outer query and scan
2112 inner_sum_func_list, it's time to modify the aggregate which was
2113 skipped by the subquery, so we pass 'skip_registered=false'.
2114
2115 - Finally, if the subquery was transformed with IN-to-EXISTS, a new
2116 HAVING condition may have been added, which contains an Item_ref to the
2117 same Item_sum; that makes a second pointer, referenced_by[1],
2118 to remember.
2119 @todo rename skip_registered to some name which better evokes
2120 "outer-ness" of the item; subquery_none exercises this function
2121 (Bug#11762); and rename referenced_by too, as it's set only for
2122 outer-aggregated items.
2123
2124 Examples of 1):
2125
2126 (1) SELECT a+FIRST_VALUE(b*SUM(c/d)) OVER (...)
2127
2128 Assume we have done fix_fields() on this SELECT list, which list is so far
2129 only '+'. This '+' contains a WF (and a group aggregate function), so the
2130 resolver (generally, Query_block::prepare()) calls Item::split_sum_func2 on
2131 the '+'; as this '+' is neither a WF nor a group aggregate, but contains
2132 some, it calls Item_func::split_sum_func which calls Item::split_sum_func2 on
2133 every argument of the '+':
2134
2135 - for 'a', it adds it to 'fields' as a hidden item
2136
2137 - then the FIRST_VALUE wf is added as a hidden item; this is necessary so
2138 that create_tmp_table() and copy_funcs can spot the WF.
2139
2140 - next, for FIRST_VALUE: it is a WF, so its Item_sum::split_sum_func is
2141 called, as its arguments need to be added as hidden items so they can get
2142 carried forward between the tmp tables. This split_sum_func calls
2143 Item::split_sum_func2 on its argument (the '*'); this
2144 '*' is not a group aggregate but contains one, so its
2145 Item_func::split_sum_func is called, which calls Item::split_sum_func2 on
2146 every argument of the '*':
2147 - for 'b', adds it to 'fields' as a hidden item
2148 - for SUM: it is a group aggregate (and doesn't contain any WF) so it
2149 adds it to 'fields' as a hidden item.
2150
2151 So we finally have, in 'fields':
2152
2153 SUM, b, FIRST_VALUE, a, +
2154
2155 Each time we add a hidden item we re-point its parent to the hidden item
2156 using an Item_aggregate_ref. For example, the args[0] of '+' is made to point
2157 to an Item_aggregate_ref which points to the hidden 'a'.
2158
2159 Examples of 2):
2160
2161 SELECT LAST_VALUE((SELECT upper.j FROM t1 LIMIT 1)) OVER (ORDER BY i)
2162 FROM t1 AS upper;
2163 */
2164
2165 776324 void Item::split_sum_func2(THD *thd, Ref_item_array ref_item_array,
2166 mem_root_deque<Item *> *fields, Item **ref,
2167 bool skip_registered) {
2168
1/2
✓ Branch 0 taken 776324 times.
✗ Branch 1 not taken.
776324 DBUG_TRACE;
2169 /* An item of type Item_sum is registered <=> referenced_by[0] != 0 */
2170
7/8
✓ Branch 0 taken 776324 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 330651 times.
✓ Branch 3 taken 445673 times.
✓ Branch 4 taken 330112 times.
✓ Branch 5 taken 539 times.
✓ Branch 6 taken 50 times.
✓ Branch 7 taken 776274 times.
1106436 if (type() == SUM_FUNC_ITEM && skip_registered &&
2171
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 330062 times.
330112 (down_cast<Item_sum *>(this))->referenced_by[0])
2172 50 return;
2173
2174 // 'sum_func' means a group aggregate function
2175
5/6
✓ Branch 0 taken 776274 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 330601 times.
✓ Branch 3 taken 445673 times.
✓ Branch 4 taken 330260 times.
✓ Branch 5 taken 341 times.
776274 const bool is_sum_func = type() == SUM_FUNC_ITEM && !m_is_window_function;
2176
4/4
✓ Branch 0 taken 136110 times.
✓ Branch 1 taken 309904 times.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 136077 times.
446014 if ((!is_sum_func && has_aggregation() && !m_is_window_function) ||
2177
4/4
✓ Branch 0 taken 639856 times.
✓ Branch 1 taken 341 times.
✓ Branch 2 taken 639792 times.
✓ Branch 3 taken 64 times.
640197 (!m_is_window_function && has_wf()) ||
2178
6/8
✓ Branch 0 taken 640133 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17482 times.
✓ Branch 3 taken 622651 times.
✓ Branch 4 taken 17482 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 17313 times.
✓ Branch 7 taken 169 times.
640133 (type() == FUNC_ITEM && ((down_cast<Item_func *>(this))->functype() ==
2179 17313 Item_func::ISNOTNULLTEST_FUNC ||
2180
3/4
✓ Branch 0 taken 17313 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17246 times.
✓ Branch 3 taken 67 times.
17313 (down_cast<Item_func *>(this))->functype() ==
2181
4/4
✓ Branch 0 taken 446014 times.
✓ Branch 1 taken 330260 times.
✓ Branch 2 taken 136394 times.
✓ Branch 3 taken 639880 times.
1552548 Item_func::TRIG_COND_FUNC)) ||
2182
3/4
✓ Branch 0 taken 639897 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 639880 times.
639897 type() == ROW_ITEM) {
2183 // Do not add item to hidden list; possibly split it
2184
1/2
✓ Branch 0 taken 136394 times.
✗ Branch 1 not taken.
136394 split_sum_func(thd, ref_item_array, fields);
2185
4/6
✓ Branch 0 taken 639880 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 309279 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5258 times.
✓ Branch 5 taken 304021 times.
949159 } else if ((type() == SUM_FUNC_ITEM || !const_for_execution()) && // (1)
2186
3/4
✓ Branch 0 taken 335859 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 59 times.
✓ Branch 3 taken 335800 times.
335859 (type() != SUBSELECT_ITEM || // (2)
2187
3/4
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✓ Branch 3 taken 18 times.
59 (down_cast<Item_subselect *>(this)->substype() ==
2188 41 Item_subselect::SINGLEROW_SUBS &&
2189 41 down_cast<Item_subselect *>(this)
2190 41 ->unit->first_query_block()
2191
6/8
✓ Branch 0 taken 309279 times.
✓ Branch 1 taken 330601 times.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 41 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 334567 times.
✓ Branch 7 taken 305313 times.
1279801 ->single_visible_field() != nullptr)) &&
2192
3/4
✓ Branch 0 taken 335841 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1282 times.
✓ Branch 3 taken 334559 times.
335841 (type() != REF_ITEM || // (3)
2193
3/4
✓ Branch 0 taken 1282 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 1274 times.
1282 (down_cast<Item_ref *>(this))->ref_type() ==
2194 Item_ref::VIEW_REF)) {
2195 /*
2196 (1) Replace item with a reference so that we can easily calculate
2197 it (in case of sum functions) or copy it (in case of fields)
2198
2199 The test above is to ensure we don't do a reference for things
2200 that are constants (INNER_TABLE_BIT is in effect a constant)
2201 or already referenced (for example an item in HAVING)
2202
2203 (2) In order to handle queries like:
2204 SELECT FIRST_VALUE((SELECT .. FROM .. LIMIT 1)) OVER (..) FROM ...;
2205 we need to move subselects to hidden fields too. But since window
2206 functions accept only single-row and single-column subqueries other
2207 types are excluded.
2208 Indeed, a subquery of another type is wrapped in Item_in_optimizer at this
2209 stage, so when splitting Item_in_optimizer, if we added the underlying
2210 Item_subselect to "fields" below it would be later evaluated by
2211 copy_funcs() (in tmp table processing), which would be incorrect as the
2212 Item_subselect cannot be evaluated - as it must always be evaluated
2213 through its parent Item_in_optimizer.
2214
2215 (3) Exception from (1) is Item_view_ref which we need to wrap in
2216 Item_ref to allow fields from view being stored in tmp table.
2217 */
2218
3/8
✓ Branch 0 taken 334567 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 334567 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 334567 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
334567 DBUG_PRINT("info", ("replacing %s with reference", item_name.ptr()));
2219
2220 334567 const bool old_hidden = hidden; // May be overwritten below.
2221
2222 // See if the item is already there. If it's not there
2223 // (the common case), we put it at the end.
2224 //
2225 // However, if a scalar-subquery-to-derived rewrite needed to process
2226 // a HAVING item, we might already be there (as a visible item).
2227 // If so, we must not add ourselves twice, or we'd overwrite the hidden
2228 // flag.
2229 uint el =
2230
1/2
✓ Branch 0 taken 334567 times.
✗ Branch 1 not taken.
334567 std::find(&ref_item_array[0], &ref_item_array[fields->size()], this) -
2231 334567 &ref_item_array[0];
2232
2/2
✓ Branch 0 taken 334538 times.
✓ Branch 1 taken 29 times.
334567 if (el == fields->size()) {
2233 // Was not there from before, so add ourselves as a hidden item.
2234 334538 ref_item_array[el] = this;
2235 // Should also be absent from 'fields', for consistency.
2236
6/12
✓ Branch 0 taken 334538 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 334538 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 334538 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 334538 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 334538 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 334538 times.
334538 assert(std::find(fields->begin(), fields->end(), this) == fields->end());
2237
1/2
✓ Branch 0 taken 334538 times.
✗ Branch 1 not taken.
334538 fields->push_front(this);
2238 334538 hidden = true;
2239 } else {
2240
6/12
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 29 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 29 times.
29 assert(std::find(fields->begin(), fields->end(), this) != fields->end());
2241 }
2242
2243 Query_block *base_query_block;
2244 334567 Query_block *depended_from = nullptr;
2245
7/8
✓ Branch 0 taken 334567 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 330601 times.
✓ Branch 3 taken 3966 times.
✓ Branch 4 taken 330260 times.
✓ Branch 5 taken 341 times.
✓ Branch 6 taken 330260 times.
✓ Branch 7 taken 4307 times.
334567 if (type() == SUM_FUNC_ITEM && !m_is_window_function) {
2246 330260 Item_sum *const item = down_cast<Item_sum *>(this);
2247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 330260 times.
330260 assert(thd->lex->current_query_block() == item->aggr_query_block);
2248 330260 base_query_block = item->base_query_block;
2249
2/2
✓ Branch 0 taken 539 times.
✓ Branch 1 taken 329721 times.
330260 if (item->aggr_query_block != base_query_block)
2250 539 depended_from = item->aggr_query_block;
2251 } else {
2252 4307 base_query_block = thd->lex->current_query_block();
2253 }
2254
2255 Item_aggregate_ref *const item_ref = new Item_aggregate_ref(
2256 334567 &base_query_block->context, &ref_item_array[el], nullptr, nullptr,
2257
2/4
✓ Branch 0 taken 334567 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 334567 times.
✗ Branch 3 not taken.
334567 item_name.ptr(), depended_from);
2258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 334567 times.
334567 if (!item_ref) return; /* purecov: inspected */
2259 334567 item_ref->hidden = old_hidden;
2260
2/2
✓ Branch 0 taken 539 times.
✓ Branch 1 taken 334028 times.
334567 if (ref == nullptr) {
2261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 539 times.
539 assert(is_sum_func);
2262 // Let 'ref' be the two elements of referenced_by[].
2263 539 ref = down_cast<Item_sum *>(this)->referenced_by[1];
2264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 539 times.
539 if (ref != nullptr) *ref = item_ref;
2265 539 ref = down_cast<Item_sum *>(this)->referenced_by[0];
2266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 539 times.
539 assert(ref);
2267 }
2268 // WL#6570 remove-after-qa
2269
3/4
✓ Branch 0 taken 3689 times.
✓ Branch 1 taken 330878 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3689 times.
334567 assert(thd->stmt_arena->is_regular() || !thd->lex->is_exec_started());
2270 334567 *ref = item_ref;
2271
2272 /*
2273 A WF must both be added to hidden list (done above), and be split so its
2274 arguments are added into the hidden list (done below):
2275 */
2276
3/4
✓ Branch 0 taken 341 times.
✓ Branch 1 taken 334226 times.
✓ Branch 2 taken 341 times.
✗ Branch 3 not taken.
334567 if (m_is_window_function) split_sum_func(thd, ref_item_array, fields);
2277 }
2278
2/2
✓ Branch 0 taken 776274 times.
✓ Branch 1 taken 50 times.
776324 }
2279
2280 2090967 static bool left_is_superset(DTCollation *left, DTCollation *right) {
2281 /* Allow convert to Unicode */
2282
2/2
✓ Branch 0 taken 1925192 times.
✓ Branch 1 taken 165775 times.
2090967 if (left->collation->state & MY_CS_UNICODE &&
2283
2/2
✓ Branch 0 taken 386949 times.
✓ Branch 1 taken 1538243 times.
1925192 (left->derivation < right->derivation ||
2284
2/2
✓ Branch 0 taken 287351 times.
✓ Branch 1 taken 99598 times.
386949 (left->derivation == right->derivation &&
2285
2/2
✓ Branch 0 taken 201856 times.
✓ Branch 1 taken 85495 times.
287351 (!(right->collation->state & MY_CS_UNICODE) ||
2286 /* The code below makes 4-byte utf8 a superset over 3-byte utf8 */
2287
2/2
✓ Branch 0 taken 129925 times.
✓ Branch 1 taken 71931 times.
201856 (left->collation->state & MY_CS_UNICODE_SUPPLEMENT &&
2288
2/2
✓ Branch 0 taken 129923 times.
✓ Branch 1 taken 2 times.
129925 !(right->collation->state & MY_CS_UNICODE_SUPPLEMENT) &&
2289
2/2
✓ Branch 0 taken 129750 times.
✓ Branch 1 taken 173 times.
129923 left->collation->mbmaxlen > right->collation->mbmaxlen &&
2290
2/2
✓ Branch 0 taken 129738 times.
✓ Branch 1 taken 12 times.
129750 left->collation->mbminlen == right->collation->mbminlen)))))
2291 1753476 return true;
2292 /* Allow convert from any Unicode to utf32 or utf8mb4 */
2293
2/2
✓ Branch 0 taken 87055 times.
✓ Branch 1 taken 250436 times.
337491 if (test_all_bits(left->collation->state,
2294 87055 MY_CS_UNICODE | MY_CS_UNICODE_SUPPLEMENT) &&
2295
2/2
✓ Branch 0 taken 52912 times.
✓ Branch 1 taken 34143 times.
87055 right->collation->state & MY_CS_UNICODE &&
2296
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 52725 times.
52912 left->derivation == right->derivation)
2297 187 return true;
2298 /* Allow convert from ASCII */
2299
2/2
✓ Branch 0 taken 144170 times.
✓ Branch 1 taken 193134 times.
337304 if (right->repertoire == MY_REPERTOIRE_ASCII &&
2300
2/2
✓ Branch 0 taken 74879 times.
✓ Branch 1 taken 69291 times.
144170 (left->derivation < right->derivation ||
2301
2/2
✓ Branch 0 taken 26860 times.
✓ Branch 1 taken 48019 times.
74879 (left->derivation == right->derivation &&
2302
2/2
✓ Branch 0 taken 26786 times.
✓ Branch 1 taken 74 times.
26860 !(left->repertoire == MY_REPERTOIRE_ASCII))))
2303 96077 return true;
2304 /* Disallow conversion otherwise */
2305 241227 return false;
2306 }
2307
2308 /**
2309 Aggregate two collations together taking
2310 into account their coercibility (aka derivation):.
2311
2312 0 == DERIVATION_EXPLICIT - an explicitly written COLLATE clause @n
2313 1 == DERIVATION_NONE - a mix of two different collations @n
2314 2 == DERIVATION_IMPLICIT - a column @n
2315 3 == DERIVATION_COERCIBLE - a string constant.
2316
2317 The most important rules are:
2318 -# If collations are the same:
2319 chose this collation, and the strongest derivation.
2320 -# If collations are different:
2321 - Character sets may differ, but only if conversion without
2322 data loss is possible. The caller provides flags whether
2323 character set conversion attempts should be done. If no
2324 flags are substituted, then the character sets must be the same.
2325 Currently processed flags are:
2326 MY_COLL_ALLOW_SUPERSET_CONV - allow conversion to a superset
2327 MY_COLL_ALLOW_COERCIBLE_CONV - allow conversion of a coercible value
2328 - two EXPLICIT collations produce an error, e.g. this is wrong:
2329 CONCAT(expr1 collate latin1_swedish_ci, expr2 collate latin1_german_ci)
2330 - the side with smaller derivation value wins,
2331 i.e. a column is stronger than a string constant,
2332 an explicit COLLATE clause is stronger than a column.
2333 - if derivations are the same, we have DERIVATION_NONE,
2334 we'll wait for an explicit COLLATE clause which possibly can
2335 come from another argument later: for example, this is valid,
2336 but we don't know yet when collecting the first two arguments:
2337 @code
2338 CONCAT(latin1_swedish_ci_column,
2339 latin1_german1_ci_column,
2340 expr COLLATE latin1_german2_ci)
2341 @endcode
2342
2343 @retval true If the two collations are incompatible and cannot be aggregated.
2344
2345 @retval false If the two collations can be aggregated, possibly with
2346 DERIVATION_NONE to indicate that they need a third explicit collation as a
2347 tiebreaker.
2348
2349 */
2350
2351 28667340 bool DTCollation::aggregate(DTCollation &dt, uint flags) {
2352
2/2
✓ Branch 0 taken 4201058 times.
✓ Branch 1 taken 24466379 times.
28667340 if (!my_charset_same(collation, dt.collation)) {
2353 /*
2354 We do allow to use binary strings (like BLOBS)
2355 together with character strings.
2356 Binaries have more precedence than a character
2357 string of the same derivation.
2358 */
2359
2/2
✓ Branch 0 taken 2011584 times.
✓ Branch 1 taken 2189474 times.
4201058 if (collation == &my_charset_bin) {
2360
2/2
✓ Branch 0 taken 1963316 times.
✓ Branch 1 taken 48268 times.
2011584 if (derivation <= dt.derivation)
2361 ; // Do nothing
2362 else {
2363 1963316 set(dt);
2364 }
2365
2/2
✓ Branch 0 taken 339602 times.
✓ Branch 1 taken 1849872 times.
2189474 } else if (dt.collation == &my_charset_bin) {
2366
2/2
✓ Branch 0 taken 4413 times.
✓ Branch 1 taken 335189 times.
339602 if (dt.derivation <= derivation) {
2367 4413 set(dt);
2368 }
2369
5/6
✓ Branch 0 taken 1849872 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1608761 times.
✓ Branch 3 taken 241103 times.
✓ Branch 4 taken 241104 times.
✓ Branch 5 taken 1608760 times.
3699736 } else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
2370 1849872 left_is_superset(this, &dt)) {
2371 // Do nothing
2372
5/6
✓ Branch 0 taken 241104 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 240981 times.
✓ Branch 3 taken 123 times.
✓ Branch 4 taken 240981 times.
✓ Branch 5 taken 123 times.
482208 } else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
2373 241104 left_is_superset(&dt, this)) {
2374 240981 set(dt);
2375
1/2
✓ Branch 0 taken 123 times.
✗ Branch 1 not taken.
123 } else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
2376
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 40 times.
123 derivation < dt.derivation &&
2377
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 dt.derivation >= DERIVATION_SYSCONST) {
2378 // Do nothing;
2379
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 } else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
2380
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 31 times.
40 dt.derivation < derivation &&
2381
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 derivation >= DERIVATION_SYSCONST) {
2382 9 set(dt);
2383 } else {
2384 // Cannot apply conversion
2385 31 set(&my_charset_bin, DERIVATION_NONE, (dt.repertoire | repertoire));
2386 31 return true;
2387 }
2388
2/2
✓ Branch 0 taken 4581525 times.
✓ Branch 1 taken 19884854 times.
24466379 } else if (derivation < dt.derivation) {
2389 // Do nothing
2390
2/2
✓ Branch 0 taken 157962 times.
✓ Branch 1 taken 4423563 times.
4581525 } else if (dt.derivation < derivation) {
2391 157962 set(dt);
2392 } else {
2393
2/2
✓ Branch 0 taken 49131 times.
✓ Branch 1 taken 4374432 times.
4423563 if (collation == dt.collation) {
2394 // Do nothing
2395 } else {
2396
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 49115 times.
49131 if (derivation == DERIVATION_EXPLICIT) {
2397 16 set(nullptr, DERIVATION_NONE, 0);
2398 16 return true;
2399 }
2400
2401 // If we have two different binary collations for the same character set,
2402 // and none of them is explicit, we don't know which to choose. For
2403 // example: utf8mb4_bin is a binary padding collation, utf8mb4_0900_bin is
2404 // a binary non-padding collation. Cannot determine if the resulting
2405 // collation should be padding or non-padding, unless they are also
2406 // aggregated with a third explicit collation.
2407
2/2
✓ Branch 0 taken 8851 times.
✓ Branch 1 taken 40264 times.
49115 if ((collation->state & MY_CS_BINSORT) &&
2408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8851 times.
8851 (dt.collation->state & MY_CS_BINSORT)) {
2409 set(DERIVATION_NONE);
2410 return false;
2411 }
2412
2413 // When aggregating a binary and a non-binary collation for the same
2414 // character set, the binary collation is preferred.
2415
2/2
✓ Branch 0 taken 8851 times.
✓ Branch 1 taken 40264 times.
49115 if (collation->state & MY_CS_BINSORT) return false;
2416
2/2
✓ Branch 0 taken 40051 times.
✓ Branch 1 taken 213 times.
40264 if (dt.collation->state & MY_CS_BINSORT) {
2417 40051 set(dt);
2418 40051 return false;
2419 }
2420 const CHARSET_INFO *bin =
2421 213 get_charset_by_csname(collation->csname, MY_CS_BINSORT, MYF(0));
2422 213 set(bin, DERIVATION_NONE);
2423 }
2424 }
2425 28618369 repertoire |= dt.repertoire;
2426 28618369 return false;
2427 }
2428
2429 /******************************/
2430 95 static void my_coll_agg_error(DTCollation &c1, DTCollation &c2,
2431 const char *fname) {
2432 190 my_error(ER_CANT_AGGREGATE_2COLLATIONS, MYF(0), c1.collation->m_coll_name,
2433 95 c1.derivation_name(), c2.collation->m_coll_name,
2434 c2.derivation_name(), fname);
2435 95 }
2436
2437 18 static void my_coll_agg_error(DTCollation &c1, DTCollation &c2, DTCollation &c3,
2438 const char *fname) {
2439 36 my_error(ER_CANT_AGGREGATE_3COLLATIONS, MYF(0), c1.collation->m_coll_name,
2440 18 c1.derivation_name(), c2.collation->m_coll_name,
2441 18 c2.derivation_name(), c3.collation->m_coll_name,
2442 c3.derivation_name(), fname);
2443 18 }
2444
2445 120 static void my_coll_agg_error(Item **args, uint count, const char *fname,
2446 int item_sep) {
2447
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 25 times.
120 if (count == 2)
2448 95 my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname);
2449
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 7 times.
25 else if (count == 3)
2450 18 my_coll_agg_error(args[0]->collation, args[item_sep]->collation,
2451 18 args[2 * item_sep]->collation, fname);
2452 else
2453 7 my_error(ER_CANT_AGGREGATE_NCOLLATIONS, MYF(0), fname);
2454 120 }
2455
2456 17969252 static bool agg_item_collations(DTCollation &c, const char *fname, Item **av,
2457 uint count, uint flags, int item_sep) {
2458 uint i;
2459 Item **arg;
2460 17969252 bool unknown_cs = false;
2461
2462 17969252 c.set(av[0]->collation);
2463
2/2
✓ Branch 0 taken 15302554 times.
✓ Branch 1 taken 17969238 times.
33271792 for (i = 1, arg = &av[item_sep]; i < count; i++, arg++) {
2464
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 15302496 times.
15302554 if (c.aggregate((*arg)->collation, flags)) {
2465
3/4
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 16 times.
44 if (c.derivation == DERIVATION_NONE && c.collation == &my_charset_bin) {
2466 31 unknown_cs = true;
2467 31 continue;
2468 }
2469 13 my_coll_agg_error(av, count, fname, item_sep);
2470 16 return true;
2471 }
2472 }
2473
2474
4/4
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 17969207 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 4 times.
17969238 if (unknown_cs && c.derivation != DERIVATION_EXPLICIT) {
2475 27 my_coll_agg_error(av, count, fname, item_sep);
2476 27 return true;
2477 }
2478
2479
4/4
✓ Branch 0 taken 6749903 times.
✓ Branch 1 taken 11219308 times.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 6749863 times.
17969211 if ((flags & MY_COLL_DISALLOW_NONE) && c.derivation == DERIVATION_NONE) {
2480 40 my_coll_agg_error(av, count, fname, item_sep);
2481 40 return true;
2482 }
2483
2484 /* If all arguments were numbers, reset to @@collation_connection */
2485
4/4
✓ Branch 0 taken 11219308 times.
✓ Branch 1 taken 6749863 times.
✓ Branch 2 taken 185300 times.
✓ Branch 3 taken 11034008 times.
17969171 if (flags & MY_COLL_ALLOW_NUMERIC_CONV && c.derivation == DERIVATION_NUMERIC)
2486 185300 c.set(Item::default_charset(), DERIVATION_COERCIBLE, MY_REPERTOIRE_NUMERIC);
2487
2488 17969171 return false;
2489 }
2490
2491 2708 bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
2492 Item **av, uint count, uint flags) {
2493 2708 return (agg_item_collations(c, fname, av, count,
2494 2708 flags | MY_COLL_DISALLOW_NONE, 1));
2495 }
2496
2497 31331270 bool agg_item_set_converter(DTCollation &coll, const char *fname, Item **args,
2498 uint nargs, uint, int item_sep, bool only_consts) {
2499 31331270 Item *safe_args[2] = {nullptr, nullptr};
2500
2501 /*
2502 For better error reporting: save the first and the second argument.
2503 We need this only if the the number of args is 3 or 2:
2504 - for a longer argument list, "Illegal mix of collations"
2505 doesn't display each argument's characteristics.
2506 - if nargs is 1, then this error cannot happen.
2507 */
2508
4/4
✓ Branch 0 taken 11266760 times.
✓ Branch 1 taken 20064510 times.
✓ Branch 2 taken 10795830 times.
✓ Branch 3 taken 470930 times.
31331270 if (nargs >= 2 && nargs <= 3) {
2509 10795830 safe_args[0] = args[0];
2510 10795830 safe_args[1] = args[item_sep];
2511 }
2512
2513
1/2
✓ Branch 0 taken 31331287 times.
✗ Branch 1 not taken.
31331270 THD *thd = current_thd;
2514
2515 uint i;
2516 Item **arg;
2517
2/2
✓ Branch 0 taken 46632435 times.
✓ Branch 1 taken 31331277 times.
77963712 for (i = 0, arg = args; i < nargs; i++, arg += item_sep) {
2518 size_t dummy_offset;
2519 // If told so (from comparison code), only add converter for const values.
2520
7/8
✓ Branch 0 taken 29115285 times.
✓ Branch 1 taken 17517150 times.
✓ Branch 2 taken 29115345 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13906774 times.
✓ Branch 5 taken 15208571 times.
✓ Branch 6 taken 13906778 times.
✓ Branch 7 taken 32725717 times.
77854779 if (only_consts && !(*arg)->const_item()) continue;
2521
3/4
✓ Branch 0 taken 32725657 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30929145 times.
✓ Branch 3 taken 1796512 times.
32725717 if (!String::needs_conversion(1, (*arg)->collation.collation,
2522 coll.collation, &dummy_offset))
2523 30929145 continue;
2524
2525 /*
2526 No needs to add converter if an "arg" is NUMERIC or DATETIME
2527 value (which is pure ASCII) and at the same time target DTCollation
2528 is ASCII-compatible. For example, no needs to rewrite:
2529 SELECT * FROM t1 WHERE datetime_field = '2010-01-01';
2530 to
2531 SELECT * FROM t1 WHERE CONVERT(datetime_field USING cs) = '2010-01-01';
2532
2533 TODO: avoid conversion of any values with
2534 repertoire ASCII and 7bit-ASCII-compatible,
2535 not only numeric/datetime origin.
2536 */
2537
2/2
✓ Branch 0 taken 293639 times.
✓ Branch 1 taken 1502873 times.
1796512 if ((*arg)->collation.derivation == DERIVATION_NUMERIC &&
2538
2/2
✓ Branch 0 taken 293638 times.
✓ Branch 1 taken 1 times.
293639 (*arg)->collation.repertoire == MY_REPERTOIRE_ASCII &&
2539
1/2
✓ Branch 0 taken 293638 times.
✗ Branch 1 not taken.
293638 !((*arg)->collation.collation->state & MY_CS_NONASCII) &&
2540
2/2
✓ Branch 0 taken 293199 times.
✓ Branch 1 taken 439 times.
293638 !(coll.collation->state & MY_CS_NONASCII))
2541 293199 continue;
2542
2543
1/2
✓ Branch 0 taken 1503342 times.
✗ Branch 1 not taken.
1503313 Item *conv = (*arg)->safe_charset_converter(thd, coll.collation);
2544 // @todo - check why the constructors may return error
2545
3/4
✓ Branch 0 taken 1503342 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1503340 times.
1503379 if (thd->is_error()) return true;
2546
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 1503218 times.
1503340 if (conv == nullptr &&
2547
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 37 times.
122 ((*arg)->collation.repertoire == MY_REPERTOIRE_ASCII))
2548
2/4
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 85 times.
✗ Branch 3 not taken.
85 conv = new Item_func_conv_charset(thd, *arg, coll.collation, true);
2549
2550
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 1503303 times.
1503340 if (conv == nullptr) {
2551
2/4
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
37 if (nargs >= 2 && nargs <= 3) {
2552 /* restore the original arguments for better error message */
2553 37 args[0] = safe_args[0];
2554 37 args[item_sep] = safe_args[1];
2555 }
2556
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 my_coll_agg_error(args, nargs, fname, item_sep);
2557 37 return true;
2558 }
2559
2560 // Update the Item pointer in-place
2561
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1503300 times.
1503303 if (thd->lex->is_exec_started())
2562
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 thd->change_item_tree(arg, conv);
2563 else
2564 1503300 *arg = conv;
2565
2566
1/2
✓ Branch 0 taken 1503303 times.
✗ Branch 1 not taken.
1503303 (*arg)->disable_constant_propagation(nullptr);
2567
2568
2/4
✓ Branch 0 taken 1503303 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1503303 times.
1503303 if (conv->fix_fields(thd, arg)) return true;
2569 }
2570
2571 31331277 return false;
2572 }
2573
2574 /*
2575 Collect arguments' character sets together.
2576 We allow to apply automatic character set conversion in some cases.
2577 The conditions when conversion is possible are:
2578 - arguments A and B have different charsets
2579 - A wins according to coercibility rules
2580 (i.e. a column is stronger than a string constant,
2581 an explicit COLLATE clause is stronger than a column)
2582 - character set of A is either superset for character set of B,
2583 or B is a string constant which can be converted into the
2584 character set of A without data loss.
2585
2586 If all of the above is true, then it's possible to convert
2587 B into the character set of A, and then compare according
2588 to the collation of A.
2589
2590 For functions with more than two arguments:
2591
2592 collect(A,B,C) ::= collect(collect(A,B),C)
2593
2594 When a character set conversion is needed, the respective Item pointer
2595 is updated in-place as a permanent transformation.
2596
2597 If the items are not consecutive (eg. args[2] and args[5]), use the
2598 item_sep argument, ie.
2599
2600 agg_item_charsets(coll, fname, &args[2], 2, flags, 3)
2601 */
2602
2603 17966544 bool agg_item_charsets(DTCollation &coll, const char *fname, Item **args,
2604 uint nargs, uint flags, int item_sep, bool only_consts) {
2605
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 17966462 times.
17966544 if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
2606 83 return true;
2607 17966462 return agg_item_set_converter(coll, fname, args, nargs, flags, item_sep,
2608 17966473 only_consts);
2609 }
2610
2611 17 void Item_ident_for_show::make_field(Send_field *tmp_field) {
2612 17 tmp_field->table_name = tmp_field->org_table_name = table_name;
2613 17 tmp_field->db_name = db_name;
2614 17 tmp_field->col_name = tmp_field->org_col_name = field->field_name;
2615 17 tmp_field->charsetnr = field->charset()->number;
2616 17 tmp_field->length = field->field_length;
2617 17 tmp_field->type = field->type();
2618 17 tmp_field->flags = field->all_flags();
2619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (field->table->is_nullable()) tmp_field->flags &= ~NOT_NULL_FLAG;
2620 17 tmp_field->decimals = field->decimals();
2621 17 tmp_field->field = false;
2622 17 }
2623
2624 17 bool Item_ident_for_show::fix_fields(THD *, Item **) {
2625 17 set_nullable(field->is_nullable());
2626 17 decimals = field->decimals();
2627 17 unsigned_flag = field->is_flag_set(UNSIGNED_FLAG);
2628 17 collation.set(field->charset(), field->derivation(), field->repertoire());
2629 17 set_data_type(field->type());
2630 17 max_length = char_to_byte_length_safe(field->char_length(),
2631 17 collation.collation->mbmaxlen);
2632
2633 17 fixed = true;
2634
2635 17 return false;
2636 }
2637
2638 /**
2639 Constructor used inside setup_wild().
2640 Item is resolved after construction.
2641 Item is supposed to have lifetime same as statement it is created within.
2642
2643 @param thd thread context
2644 @param context_arg Name resolution context for this field
2645 @param tr Table reference, provides table and schema name
2646 @param f Field reference, provides field name and original table name
2647 */
2648
2649 8655534 Item_field::Item_field(THD *thd, Name_resolution_context *context_arg,
2650 8655534 TABLE_LIST *tr, Field *f)
2651 8655534 : Item_ident(context_arg, f->table->s->db.str, *f->table_name,
2652 f->field_name),
2653 8655601 table_ref(tr),
2654 8655601 field(nullptr),
2655 8655601 item_equal(nullptr),
2656 8655601 field_index(NO_FIELD_INDEX),
2657 8655601 have_privileges(0),
2658 8655534 any_privileges(false) {
2659
1/2
✓ Branch 0 taken 8655348 times.
✗ Branch 1 not taken.
8655601 set_field(f);
2660
2661 // Possibly override original names that were assigned from table reference:
2662
2/2
✓ Branch 0 taken 398341 times.
✓ Branch 1 taken 8257007 times.
8655348 if (f->orig_table_name != nullptr) m_orig_table_name = f->orig_table_name;
2663
2/2
✓ Branch 0 taken 398341 times.
✓ Branch 1 taken 8257007 times.
8655348 if (f->orig_db_name != nullptr) m_orig_db_name = f->orig_db_name;
2664 /*
2665 The field pointer may have shorter lifetime than the Item that is created
2666 here, so ensure the name is created in durable memory.
2667 */
2668
1/2
✓ Branch 0 taken 8655389 times.
✗ Branch 1 not taken.
8655348 m_orig_field_name = thd->mem_strdup(f->field_name);
2669 8655389 field_name = m_orig_field_name;
2670 8655389 item_name.set(m_orig_field_name);
2671 8655551 }
2672
2673 /**
2674 Constructor used for internal information queries.
2675
2676 @param context_arg Name resolution context
2677 @param db_arg Schema name, may be NULL
2678 @param table_name_arg Table name, may be NULL if schema name is NULL
2679 @param field_name_arg Field name
2680 */
2681 3942 Item_field::Item_field(Name_resolution_context *context_arg, const char *db_arg,
2682 3942 const char *table_name_arg, const char *field_name_arg)
2683 : Item_ident(context_arg, db_arg, table_name_arg, field_name_arg),
2684 3942 table_ref(nullptr),
2685 3942 field(nullptr),
2686 3942 item_equal(nullptr),
2687 3942 field_index(NO_FIELD_INDEX),
2688 3942 have_privileges(0),
2689 3942 any_privileges(false) {
2690
1/2
✓ Branch 0 taken 3942 times.
✗ Branch 1 not taken.
3942 Query_block *select = current_thd->lex->current_query_block();
2691 3942 collation.set(DERIVATION_IMPLICIT);
2692
2/4
✓ Branch 0 taken 3942 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3942 times.
✗ Branch 3 not taken.
3942 if (select && select->parsing_place != CTX_HAVING)
2693 3942 select->select_n_where_fields++;
2694 3942 }
2695
2696 /**
2697 Used from parser to construct column references.
2698
2699 @param pos Parse context
2700 @param db_arg Schema name for column, may be NULL
2701 @param table_name_arg Table name for column, may be NULL if db_arg is NULL
2702 @param field_name_arg Column name, always given.
2703 */
2704 59286654 Item_field::Item_field(const POS &pos, const char *db_arg,
2705 59286654 const char *table_name_arg, const char *field_name_arg)
2706 : Item_ident(pos, db_arg, table_name_arg, field_name_arg),
2707 59286741 table_ref(nullptr),
2708 59286741 field(nullptr),
2709 59286741 item_equal(nullptr),
2710 59286741 field_index(NO_FIELD_INDEX),
2711 59286741 have_privileges(0),
2712 59286654 any_privileges(false) {
2713 59286741 collation.set(DERIVATION_IMPLICIT);
2714 59286706 }
2715
2716 59286387 bool Item_field::itemize(Parse_context *pc, Item **res) {
2717
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 59286427 times.
59286387 if (skip_itemize(res)) return false;
2718
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59286485 times.
59286427 if (super::itemize(pc, res)) return true;
2719 59286485 Query_block *const select = pc->select;
2720
2/2
✓ Branch 0 taken 59283378 times.
✓ Branch 1 taken 3107 times.
59286485 if (select->parsing_place != CTX_HAVING) select->select_n_where_fields++;
2721 59286485 return false;
2722 }
2723
2724 /**
2725 Used to create a copy (clone) of another Item_field.
2726 Item has same lifetime as the copied item.
2727
2728 @param thd thread handler
2729 @param item Column reference to make a copy from.
2730 */
2731
2732 238193 Item_field::Item_field(THD *thd, Item_field *item)
2733 : Item_ident(thd, item),
2734 238188 table_ref(item->table_ref),
2735 238188 field(item->field),
2736 238188 result_field(item->result_field),
2737 238188 item_equal(item->item_equal),
2738 238188 field_index(item->field_index),
2739 238188 no_constant_propagation(item->no_constant_propagation),
2740 238188 have_privileges(item->have_privileges),
2741 238193 any_privileges(item->any_privileges) {
2742 238188 collation.set(DERIVATION_IMPLICIT);
2743
2/2
✓ Branch 0 taken 233045 times.
✓ Branch 1 taken 5145 times.
238190 if (item->m_orig_table_name != nullptr)
2744 233045 m_orig_table_name = item->m_orig_table_name;
2745 else
2746 5145 m_orig_table_name = nullptr;
2747
1/2
✓ Branch 0 taken 238192 times.
✗ Branch 1 not taken.
238190 set_base_item_field(item);
2748 238192 }
2749
2750 /**
2751 Create column reference based on a table field.
2752
2753 @param f Pointer to field in a TABLE object
2754
2755 Item is resolved after construction.
2756 Notice that lifetime of object is limited to the lifetime of the
2757 supplied field.
2758 */
2759 2972527 Item_field::Item_field(Field *f)
2760 2972527 : Item_ident(nullptr, nullptr, *f->table_name, f->field_name),
2761 2972530 table_ref(nullptr),
2762 2972530 field(nullptr),
2763 2972530 item_equal(nullptr),
2764 2972530 field_index(NO_FIELD_INDEX),
2765 2972530 have_privileges(0),
2766 2972527 any_privileges(false) {
2767
2/2
✓ Branch 0 taken 114768 times.
✓ Branch 1 taken 2857762 times.
2972530 if (f->table->pos_in_table_list != nullptr)
2768 114768 context = &(f->table->pos_in_table_list->query_block->context);
2769
2770
1/2
✓ Branch 0 taken 2972526 times.
✗ Branch 1 not taken.
2972530 set_field(f);
2771 2972526 }
2772
2773 /**
2774 Calculate the max column length not taking into account the
2775 limitations over integer types.
2776
2777 When storing data into fields the server currently just ignores the
2778 limits specified on integer types, e.g. 1234 can safely be stored in
2779 an int(2) and will not cause an error.
2780 Thus when creating temporary tables and doing transformations
2781 we must adjust the maximum field length to reflect this fact.
2782 We take the un-restricted maximum length and adjust it similarly to
2783 how the declared length is adjusted wrt unsignedness etc.
2784 TODO: this all needs to go when we disable storing 1234 in int(2).
2785
2786 @param field_par Original field the use to calculate the lengths
2787 @param max_length Item's calculated explicit max length
2788 @return The adjusted max length
2789 */
2790
2791 64879492 inline static uint32 adjust_max_effective_column_length(Field *field_par,
2792 uint32 max_length) {
2793 64879492 uint32 new_max_length = field_par->max_display_length();
2794
2/2
✓ Branch 0 taken 19191375 times.
✓ Branch 1 taken 45688192 times.
64879519 uint32 sign_length = field_par->is_flag_set(UNSIGNED_FLAG) ? 0 : 1;
2795
2796
3/3
✓ Branch 0 taken 55954 times.
✓ Branch 1 taken 14200915 times.
✓ Branch 2 taken 50622714 times.
64879567 switch (field_par->type()) {
2797 55954 case MYSQL_TYPE_INT24:
2798 /*
2799 Compensate for MAX_MEDIUMINT_WIDTH being 1 too long (8)
2800 compared to the actual number of digits that can fit into
2801 the column.
2802 */
2803 55954 new_max_length += 1;
2804 [[fallthrough]];
2805 14256869 case MYSQL_TYPE_LONG:
2806 case MYSQL_TYPE_TINY:
2807 case MYSQL_TYPE_SHORT:
2808
2809 /* Take out the sign and add a conditional sign */
2810 14256869 new_max_length = new_max_length - 1 + sign_length;
2811 14256869 break;
2812
2813 /* BINGINT is always 20 no matter the sign */
2814 50622714 case MYSQL_TYPE_LONGLONG:
2815 /* make gcc happy */
2816 default:
2817 50622714 break;
2818 }
2819
2820 /* Adjust only if the actual precision based one is bigger than specified */
2821
2/2
✓ Branch 0 taken 430471 times.
✓ Branch 1 taken 64449112 times.
64879583 return new_max_length > max_length ? new_max_length : max_length;
2822 }
2823
2824 64879670 void Item_field::set_field(Field *field_par) {
2825 64879670 table_ref = field_par->table->pos_in_table_list;
2826
3/4
✓ Branch 0 taken 61915924 times.
✓ Branch 1 taken 2963746 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 61915924 times.
64879670 assert(table_ref == nullptr || table_ref->table == field_par->table);
2827
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64879748 times.
64879670 assert(field_par->field_index() != NO_FIELD_INDEX);
2828 64879748 field_index = field_par->field_index();
2829
2830 64879774 field = result_field = field_par; // for easy coding with fields
2831
6/6
✓ Branch 0 taken 43709840 times.
✓ Branch 1 taken 21169890 times.
✓ Branch 2 taken 43675909 times.
✓ Branch 3 taken 33930 times.
✓ Branch 4 taken 2680909 times.
✓ Branch 5 taken 40995019 times.
108555702 set_nullable(field->is_nullable() || field->is_tmp_nullable() ||
2832 43675909 field->table->is_nullable());
2833
2/2
✓ Branch 0 taken 61915850 times.
✓ Branch 1 taken 2963796 times.
64879646 if (table_ref != nullptr) {
2834 61915850 table_name = table_ref->alias;
2835 61915850 m_orig_db_name = table_ref->db;
2836 61915850 db_name = m_orig_db_name;
2837 61915850 m_orig_table_name = table_ref->table_name;
2838
2/2
✓ Branch 0 taken 560594 times.
✓ Branch 1 taken 61355354 times.
61915850 if (table_ref->is_derived()) {
2839 // Show underlying field's information
2840 560594 m_orig_db_name = field_par->orig_db_name;
2841 560594 m_orig_table_name = field_par->orig_table_name;
2842 }
2843 } else {
2844 2963796 m_orig_db_name = field_par->orig_db_name;
2845 2963796 db_name = m_orig_db_name;
2846 2963796 m_orig_table_name = field_par->orig_table_name;
2847 2963796 table_name = m_orig_table_name;
2848 }
2849
2850 64879744 m_orig_field_name = field_par->field_name;
2851 64879536 collation.set(field_par->charset(), field_par->derivation(),
2852 64879744 field_par->repertoire());
2853 64879569 set_data_type(field_par->type());
2854 64879538 decimals = field->decimals();
2855 64879595 unsigned_flag = field_par->is_flag_set(UNSIGNED_FLAG);
2856 64879680 max_length = char_to_byte_length_safe(field_par->char_length(),
2857 64879552 collation.collation->mbmaxlen);
2858
2859 64879537 max_length = adjust_max_effective_column_length(field_par, max_length);
2860
2861
2/2
✓ Branch 0 taken 1182756 times.
✓ Branch 1 taken 63696875 times.
64879631 if (field->table->s->tmp_table == SYSTEM_TMP_TABLE) any_privileges = false;
2862
2/2
✓ Branch 0 taken 64468807 times.
✓ Branch 1 taken 410824 times.
64879631 if (!can_use_prefix_key)
2863 64468807 field->table->covering_keys.subtract(field->part_of_prefixkey);
2864
2865 64879409 fixed = true;
2866 64879409 }
2867
2868 /**
2869 Reset this item to point to a field from the new temporary table.
2870 This is used when we create a new temporary table for each execution
2871 of prepared statement.
2872 */
2873
2874 170 void Item_field::reset_field(Field *f) {
2875 170 set_field(f);
2876 /* 'name' is pointing at field->field_name of old field */
2877 170 item_name.set(f->field_name);
2878 170 }
2879
2880 17949 const char *Item_ident::full_name() const {
2881 17949 const char *f_name =
2882
1/2
✓ Branch 0 taken 17949 times.
✗ Branch 1 not taken.
17949 m_orig_field_name != nullptr ? m_orig_field_name : field_name;
2883 char *tmp;
2884
3/4
✓ Branch 0 taken 16527 times.
✓ Branch 1 taken 1422 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16527 times.
17949 if (table_name == nullptr || f_name == nullptr)
2885 return f_name != nullptr
2886
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1422 times.
1422 ? f_name
2887
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
1422 : item_name.is_set() ? item_name.ptr() : "tmp_field";
2888
4/4
✓ Branch 0 taken 16354 times.
✓ Branch 1 taken 173 times.
✓ Branch 2 taken 16023 times.
✓ Branch 3 taken 331 times.
16527 if (db_name && db_name[0]) {
2889 16023 tmp = pointer_cast<char *>(
2890 16023 (*THR_MALLOC)
2891 16023 ->Alloc(strlen(db_name) + strlen(table_name) + strlen(f_name) + 3));
2892 16023 strxmov(tmp, db_name, ".", table_name, ".", f_name, NullS);
2893 } else {
2894
1/2
✓ Branch 0 taken 504 times.
✗ Branch 1 not taken.
504 if (table_name[0]) {
2895 504 tmp = pointer_cast<char *>(
2896 504 (*THR_MALLOC)->Alloc(strlen(table_name) + strlen(f_name) + 2));
2897 504 strxmov(tmp, table_name, ".", f_name, NullS);
2898 } else
2899 return f_name;
2900 }
2901 16527 return tmp;
2902 }
2903
2904 4380421 void Item_ident::print(const THD *thd, String *str, enum_query_type query_type,
2905 const char *db_name_arg,
2906 const char *table_name_arg) const {
2907 char d_name_buff[MAX_ALIAS_NAME], t_name_buff[MAX_ALIAS_NAME];
2908 4380421 const char *d_name = db_name_arg;
2909 4380421 const char *t_name = table_name_arg;
2910 4380421 const char *f_name =
2911
2/2
✓ Branch 0 taken 4379655 times.
✓ Branch 1 taken 766 times.
4380421 m_orig_field_name != nullptr ? m_orig_field_name : field_name;
2912
2913
4/4
✓ Branch 0 taken 3928310 times.
✓ Branch 1 taken 452111 times.
✓ Branch 2 taken 452111 times.
✓ Branch 3 taken 3928310 times.
8308731 if (lower_case_table_names == 1 ||
2914 // mode '2' does not apply to aliases:
2915
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 3928310 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
3928310 (lower_case_table_names == 2 && !alias_name_used())) {
2916
4/4
✓ Branch 0 taken 451204 times.
✓ Branch 1 taken 907 times.
✓ Branch 2 taken 450540 times.
✓ Branch 3 taken 664 times.
452111 if (table_name_arg && table_name_arg[0]) {
2917 450540 my_stpcpy(t_name_buff, table_name_arg);
2918
1/2
✓ Branch 0 taken 450540 times.
✗ Branch 1 not taken.
450540 my_casedn_str(files_charset_info, t_name_buff);
2919 450540 t_name = t_name_buff;
2920 }
2921
4/4
✓ Branch 0 taken 451204 times.
✓ Branch 1 taken 907 times.
✓ Branch 2 taken 437734 times.
✓ Branch 3 taken 13470 times.
452111 if (db_name_arg && db_name_arg[0]) {
2922 437734 my_stpcpy(d_name_buff, db_name_arg);
2923
1/2
✓ Branch 0 taken 437734 times.
✗ Branch 1 not taken.
437734 my_casedn_str(files_charset_info, d_name_buff);
2924 437734 d_name = d_name_buff;
2925 }
2926 }
2927
2928
5/6
✓ Branch 0 taken 4342760 times.
✓ Branch 1 taken 37661 times.
✓ Branch 2 taken 4342760 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 4342758 times.
4380421 if (table_name_arg == nullptr || f_name == nullptr || !f_name[0]) {
2929
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 37622 times.
37624 const char *nm = (f_name != nullptr && f_name[0])
2930
2/2
✓ Branch 0 taken 37624 times.
✓ Branch 1 taken 39 times.
75326 ? f_name
2931
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 39 times.
41 : item_name.is_set() ? item_name.ptr() : "tmp_field";
2932
1/2
✓ Branch 0 taken 37663 times.
✗ Branch 1 not taken.
37663 append_identifier(thd, str, nm, strlen(nm));
2933 37663 return;
2934 }
2935
2936
8/8
✓ Branch 0 taken 4341745 times.
✓ Branch 1 taken 1013 times.
✓ Branch 2 taken 4341689 times.
✓ Branch 3 taken 56 times.
✓ Branch 4 taken 4173182 times.
✓ Branch 5 taken 168507 times.
✓ Branch 6 taken 2507481 times.
✓ Branch 7 taken 1835277 times.
8515940 if (!(query_type & QT_NO_DB) && db_name_arg && db_name_arg[0] &&
2937
3/4
✓ Branch 0 taken 4173182 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2507481 times.
✓ Branch 3 taken 1665701 times.
4173182 !alias_name_used()) {
2938 2507481 const size_t d_name_len = strlen(d_name);
2939
4/4
✓ Branch 0 taken 51480 times.
✓ Branch 1 taken 2456001 times.
✓ Branch 2 taken 2466026 times.
✓ Branch 3 taken 41455 times.
2558961 if (!((query_type & QT_NO_DEFAULT_DB) &&
2940
3/4
✓ Branch 0 taken 51480 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10025 times.
✓ Branch 3 taken 41455 times.
51480 db_is_default_db(d_name, d_name_len, thd))) {
2941
1/2
✓ Branch 0 taken 2466026 times.
✗ Branch 1 not taken.
2466026 append_identifier(thd, str, d_name, d_name_len);
2942
1/2
✓ Branch 0 taken 2466026 times.
✗ Branch 1 not taken.
2466026 str->append('.');
2943 }
2944 }
2945
4/4
✓ Branch 0 taken 4341745 times.
✓ Branch 1 taken 1013 times.
✓ Branch 2 taken 4329668 times.
✓ Branch 3 taken 12077 times.
4342758 if (!(query_type & QT_NO_TABLE) && table_name_arg[0]) {
2946
1/2
✓ Branch 0 taken 4329668 times.
✗ Branch 1 not taken.
4329668 append_identifier(thd, str, t_name, strlen(t_name));
2947
1/2
✓ Branch 0 taken 4329668 times.
✗ Branch 1 not taken.
4329668 str->append('.');
2948 }
2949
1/2
✓ Branch 0 taken 4342758 times.
✗ Branch 1 not taken.
4342758 append_identifier(thd, str, f_name, strlen(f_name));
2950 }
2951
2952 537 TYPELIB *Item_field::get_typelib() const {
2953 537 return down_cast<Field_enum *>(field)->typelib;
2954 }
2955
2956 621911251 String *Item_field::val_str(String *str) {
2957
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 621911251 times.
621911251 assert(fixed == 1);
2958
2/2
✓ Branch 0 taken 25557493 times.
✓ Branch 1 taken 596353822 times.
621911251 if ((null_value = field->is_null())) return nullptr;
2959 596353822 str->set_charset(str_value.charset());
2960 596353819 return field->val_str(str, &str_value);
2961 }
2962
2963 507376 bool Item_field::val_json(Json_wrapper *result) {
2964
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 507376 times.
507376 assert(fixed);
2965
3/4
✓ Branch 0 taken 1116 times.
✓ Branch 1 taken 506288 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1116 times.
507376 assert(data_type() == MYSQL_TYPE_JSON || returns_array());
2966 507404 null_value = field->is_null();
2967
2/2
✓ Branch 0 taken 2227 times.
✓ Branch 1 taken 505179 times.
507406 if (null_value) return false;
2968 505179 return down_cast<Field_json *>(field)->val_json(result);
2969 }
2970
2971 11960233 double Item_field::val_real() {
2972
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11960233 times.
11960233 assert(fixed == 1);
2973
2/2
✓ Branch 0 taken 46388 times.
✓ Branch 1 taken 11913846 times.
11960233 if ((null_value = field->is_null())) return 0.0;
2974 11913846 return field->val_real();
2975 }
2976
2977 349355162 longlong Item_field::val_int() {
2978
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 349355162 times.
349355162 assert(fixed == 1);
2979
2/2
✓ Branch 0 taken 13021772 times.
✓ Branch 1 taken 336334088 times.
349355162 if ((null_value = field->is_null())) return 0;
2980 336334088 return field->val_int();
2981 }
2982
2983 10939 longlong Item_field::val_time_temporal() {
2984
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10939 times.
10939 assert(fixed == 1);
2985
2/2
✓ Branch 0 taken 320 times.
✓ Branch 1 taken 10619 times.
10939 if ((null_value = field->is_null())) return 0;
2986 10619 return field->val_time_temporal();
2987 }
2988
2989 339010 longlong Item_field::val_date_temporal() {
2990
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 339010 times.
339010 assert(fixed == 1);
2991
2/2
✓ Branch 0 taken 1027 times.
✓ Branch 1 taken 337983 times.
339010 if ((null_value = field->is_null())) return 0;
2992 337983 return field->val_date_temporal();
2993 }
2994
2995 10549 longlong Item_field::val_time_temporal_at_utc() {
2996
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10549 times.
10549 assert(fixed == 1);
2997
2/2
✓ Branch 0 taken 235 times.
✓ Branch 1 taken 10314 times.
10549 if ((null_value = field->is_null())) return 0;
2998 10314 return field->val_time_temporal_at_utc();
2999 }
3000
3001 80495 longlong Item_field::val_date_temporal_at_utc() {
3002
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80495 times.
80495 assert(fixed == 1);
3003
2/2
✓ Branch 0 taken 9835 times.
✓ Branch 1 taken 70660 times.
80495 if ((null_value = field->is_null())) return 0;
3004 70660 return field->val_date_temporal_at_utc();
3005 }
3006
3007 40879169 my_decimal *Item_field::val_decimal(my_decimal *decimal_value) {
3008 40879169 null_value = field->is_null();
3009
2/2
✓ Branch 0 taken 391248 times.
✓ Branch 1 taken 40487926 times.
40879174 if (null_value) return nullptr;
3010 40487926 return field->val_decimal(decimal_value);
3011 }
3012
3013 31919 bool Item_field::get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) {
3014
6/6
✓ Branch 0 taken 30819 times.
✓ Branch 1 taken 1100 times.
✓ Branch 2 taken 695 times.
✓ Branch 3 taken 30124 times.
✓ Branch 4 taken 1795 times.
✓ Branch 5 taken 30124 times.
31919 if ((null_value = field->is_null()) || field->get_date(ltime, fuzzydate)) {
3015 1795 memset(ltime, 0, sizeof(*ltime));
3016 1795 return true;
3017 }
3018 30124 return false;
3019 }
3020
3021 5131 bool Item_field::get_time(MYSQL_TIME *ltime) {
3022
6/6
✓ Branch 0 taken 5000 times.
✓ Branch 1 taken 131 times.
✓ Branch 2 taken 145 times.
✓ Branch 3 taken 4855 times.
✓ Branch 4 taken 276 times.
✓ Branch 5 taken 4855 times.
5131 if ((null_value = field->is_null()) || field->get_time(ltime)) {
3023 276 memset(ltime, 0, sizeof(*ltime));
3024 276 return true;
3025 }
3026 4855 return false;
3027 }
3028
3029 2038 bool Item_field::get_timeval(my_timeval *tm, int *warnings) {
3030
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 1993 times.
2038 if ((null_value = field->is_null())) return true;
3031
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1974 times.
1993 if (field->get_timestamp(tm, warnings)) tm->m_tv_sec = tm->m_tv_usec = 0;
3032 1993 return false;
3033 }
3034
3035 11919869 bool Item_field::eq(const Item *item, bool) const {
3036 11919869 const Item *real_item = item->real_item();
3037
2/2
✓ Branch 0 taken 2881668 times.
✓ Branch 1 taken 9038247 times.
11919874 if (real_item->type() != FIELD_ITEM) return false;
3038
3039 9038247 const Item_field *item_field = down_cast<const Item_field *>(real_item);
3040
3041 /*
3042 If both Item_field objects are properly resolved, return true if they both
3043 refer to the same underlying table field. If one or both fields refer to
3044 temporary table fields derived from some base table field, return true
3045 also if they refer to the same base table field.
3046 The original table's name and original field's name cannot serve here,
3047 consider: SELECT a FROM t1 WHERE b IN (SELECT a FROM t1)
3048 where the semijoin-merged 'a' and the top query's 'a' are both named t1.a
3049 and coexist in the top query.
3050 */
3051
4/4
✓ Branch 0 taken 8804501 times.
✓ Branch 1 taken 233706 times.
✓ Branch 2 taken 8804234 times.
✓ Branch 3 taken 267 times.
9038207 if (fixed && item_field->fixed)
3052 8804234 return base_item_field()->field == item_field->base_item_field()->field;
3053 /*
3054 We may come here when we are trying to find a function in a GROUP BY
3055 clause from the select list.
3056 In this case the '100 % correct' way to do this would be to first
3057 run fix_fields() on the GROUP BY item and then retry this function, but
3058 I think it's better to relax the checking a bit as we will in
3059 most cases do the correct thing by just checking the field name.
3060 (In cases where we would choose wrong we would have to generate a
3061 ER_NON_UNIQ_ERROR).
3062 */
3063
2/2
✓ Branch 0 taken 115776 times.
✓ Branch 1 taken 118226 times.
349749 return (item_field->item_name.eq_safe(field_name) &&
3064
4/4
✓ Branch 0 taken 115492 times.
✓ Branch 1 taken 284 times.
✓ Branch 2 taken 99320 times.
✓ Branch 3 taken 16172 times.
115776 (!item_field->table_name || !table_name ||
3065
2/2
✓ Branch 0 taken 99318 times.
✓ Branch 1 taken 2 times.
99320 (!my_strcasecmp(table_alias_charset, item_field->table_name,
3066 99318 table_name) &&
3067
3/4
✓ Branch 0 taken 99318 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44213 times.
✓ Branch 3 taken 55105 times.
99318 (!item_field->db_name || !db_name ||
3068
2/4
✓ Branch 0 taken 44213 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44213 times.
✗ Branch 3 not taken.
278215 (item_field->db_name && !strcmp(item_field->db_name, db_name))))));
3069 }
3070
3071 4557739597 table_map Item_field::used_tables() const {
3072
2/2
✓ Branch 0 taken 255945 times.
✓ Branch 1 taken 4557483652 times.
4557739597 if (!table_ref) return 1; // Temporary table; always table 0
3073
2/2
✓ Branch 0 taken 361370 times.
✓ Branch 1 taken 4557122282 times.
4557483652 if (table_ref->table->const_table) return 0; // const item
3074
2/2
✓ Branch 0 taken 4556919923 times.
✓ Branch 1 taken 202359 times.
4557122282 return depended_from ? OUTER_REF_TABLE_BIT : table_ref->map();
3075 }
3076
3077 11617 bool Item_field::used_tables_for_level(uchar *arg) {
3078 11617 const TABLE_LIST *tr = field->table->pos_in_table_list;
3079 // Used by resolver only, so can never reach a "const" table.
3080
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11617 times.
11617 assert(!tr->table->const_table);
3081 11617 Used_tables *const ut = pointer_cast<Used_tables *>(arg);
3082 /*
3083 When the qualifying query for the field (table_ref->query_block) is the same
3084 level as the requested level, add the table's map.
3085 When the qualifying query for the field is outer relative to the
3086 requested level, add an outer reference.
3087 */
3088
2/2
✓ Branch 0 taken 11589 times.
✓ Branch 1 taken 28 times.
11617 if (ut->select == tr->query_block)
3089 11589 ut->used_tables |= tr->map();
3090
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 10 times.
28 else if (ut->select->nest_level > tr->query_block->nest_level)
3091 18 ut->used_tables |= OUTER_REF_TABLE_BIT;
3092
3093 11617 return false;
3094 }
3095
3096 33994092 void Item_ident::fix_after_pullout(Query_block *parent_query_block,
3097 Query_block *removed_query_block) {
3098 /*
3099 Some field items may be created for use in execution only, without
3100 a name resolution context. They have already been used in execution,
3101 so no transformation is necessary here.
3102
3103 @todo: Provide strict phase-division in optimizer, to make sure that
3104 execution-only objects do not exist during transformation stage.
3105 Then, this test would be deemed unnecessary.
3106 */
3107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33994092 times.
33994092 if (context == nullptr) {
3108 assert(type() == FIELD_ITEM);
3109 return;
3110 }
3111
3112 // context->query_block should already have been updated.
3113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33994092 times.
33994092 assert(context->query_block != removed_query_block);
3114
3115
2/2
✓ Branch 0 taken 33979781 times.
✓ Branch 1 taken 14311 times.
33994092 if (context->query_block == parent_query_block) {
3116
2/2
✓ Branch 0 taken 2985 times.
✓ Branch 1 taken 33976796 times.
33979781 if (parent_query_block == depended_from) {
3117 2985 depended_from = nullptr;
3118 // Update the context of this field to that of the parent query
3119 // block since the resolver place is now lifted from the abandoned
3120 // query block to this one.
3121 2985 context = &parent_query_block->context;
3122 }
3123 } else {
3124 /*
3125 The definition scope of this field item reference is inner to the removed
3126 query_block object.
3127 No new resolution is needed, but we may need to update the dependency.
3128 */
3129
2/2
✓ Branch 0 taken 1847 times.
✓ Branch 1 taken 12464 times.
14311 if (removed_query_block == depended_from)
3130 1847 depended_from = parent_query_block;
3131 }
3132
3133
2/2
✓ Branch 0 taken 2347 times.
✓ Branch 1 taken 33991745 times.
33994092 if (depended_from) {
3134 /*
3135 Refresh used_tables information for subqueries between the definition
3136 scope and resolution scope of the field item reference.
3137 */
3138 2347 Query_block *child_query_block = context->query_block;
3139
3140
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 2347 times.
2429 while (child_query_block->outer_query_block() != depended_from) {
3141 /*
3142 The subquery on this level is outer-correlated with respect to the field
3143 */
3144
1/2
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
82 child_query_block->master_query_expression()->accumulate_used_tables(
3145 OUTER_REF_TABLE_BIT);
3146 82 child_query_block = child_query_block->outer_query_block();
3147 }
3148
3149 /*
3150 child_query_block is query_block immediately inner to the depended_from
3151 level. Now, locate the subquery predicate that contains this query_block
3152 and update used tables information.
3153 */
3154 2347 Used_tables ut(depended_from);
3155
1/2
✓ Branch 0 taken 2347 times.
✗ Branch 1 not taken.
2347 (void)walk(&Item::used_tables_for_level, enum_walk::SUBQUERY_POSTFIX,
3156 pointer_cast<uchar *>(&ut));
3157
1/2
✓ Branch 0 taken 2347 times.
✗ Branch 1 not taken.
2347 child_query_block->master_query_expression()->accumulate_used_tables(
3158 ut.used_tables);
3159 }
3160 }
3161
3162 57377 Item *Item_field::get_tmp_table_item(THD *thd) {
3163
1/2
✓ Branch 0 taken 57377 times.
✗ Branch 1 not taken.
57377 DBUG_TRACE;
3164
2/4
✓ Branch 0 taken 57377 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57377 times.
✗ Branch 3 not taken.
57377 Item_field *new_item = new Item_field(thd, this);
3165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57377 times.
57377 if (!new_item) return nullptr; /* purecov: inspected */
3166
3167 57377 new_item->field = new_item->result_field;
3168 57377 new_item->table_ref = nullptr; // Internal temporary table has no table_ref
3169
3170 57377 return new_item;
3171 57377 }
3172
3173 33560 longlong Item_field::val_int_endpoint(bool, bool *) {
3174 33560 longlong res = val_int();
3175
2/2
✓ Branch 0 taken 633 times.
✓ Branch 1 taken 32927 times.
33560 return null_value ? LLONG_MIN : res;
3176 }
3177
3178 /**
3179 Init an item from a string we KNOW points to a valid longlong.
3180 str_arg does not necessary has to be a \\0 terminated string.
3181 This is always 'signed'. Unsigned values are created with Item_uint()
3182 */
3183 62627 void Item_int::init(const char *str_arg, uint length) {
3184 62627 const char *end_ptr = str_arg + length;
3185 int error;
3186
1/2
✓ Branch 0 taken 62629 times.
✗ Branch 1 not taken.
62627 value = my_strtoll10(str_arg, &end_ptr, &error);
3187 62629 set_max_size(static_cast<uint>(end_ptr - str_arg));
3188
1/2
✓ Branch 0 taken 62627 times.
✗ Branch 1 not taken.
62627 item_name.copy(str_arg, max_length);
3189 62627 fixed = true;
3190 62627 }
3191
3192 2527012 my_decimal *Item_int::val_decimal(my_decimal *decimal_value) {
3193 2527012 int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_value);
3194 2527021 return decimal_value;
3195 }
3196
3197 93583 String *Item_int::val_str(String *str) {
3198 // following assert is redundant, because fixed=1 assigned in constructor
3199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93583 times.
93583 assert(fixed == 1);
3200 93583 str->set_int(value, unsigned_flag, collation.collation);
3201 93583 return str;
3202 }
3203
3204 792917 void Item_int::print(const THD *, String *str,
3205 enum_query_type query_type) const {
3206
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 792661 times.
792917 if (query_type & QT_NORMALIZED_FORMAT) {
3207
1/2
✓ Branch 0 taken 256 times.
✗ Branch 1 not taken.
256 str->append("?");
3208 256 return;
3209 }
3210 // my_charset_bin is good enough for numbers
3211
3212 // don't rewrite booleans as ints. see bug#21296173
3213 792661 const Name_string *const name = &item_name;
3214
3/4
✓ Branch 0 taken 729365 times.
✓ Branch 1 taken 63296 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 729365 times.
792661 const bool is_literal_false = name->is_set() && name->eq("FALSE");
3215
3/4
✓ Branch 0 taken 729365 times.
✓ Branch 1 taken 63296 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 729365 times.
792661 const bool is_literal_true = name->is_set() && name->eq("TRUE");
3216
2/4
✓ Branch 0 taken 792661 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 792661 times.
792661 if (is_literal_false || is_literal_true) {
3217 str->append(item_name.ptr(), item_name.length(), str->charset());
3218 } else {
3219
2/2
✓ Branch 0 taken 710 times.
✓ Branch 1 taken 791951 times.
792661 if (unsigned_flag)
3220 710 str->append_ulonglong(value);
3221 else
3222 791951 str->append_longlong(value);
3223 }
3224 }
3225
3226 15 String *Item_uint::val_str(String *str) {
3227 // following assert is redundant, because fixed=1 assigned in constructor
3228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 assert(fixed == 1);
3229 15 str->set((ulonglong)value, collation.collation);
3230 15 return str;
3231 }
3232
3233 12714 void Item_uint::print(const THD *, String *str,
3234 enum_query_type query_type) const {
3235
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 12709 times.
12714 if (query_type & QT_NORMALIZED_FORMAT) {
3236
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 str->append("?");
3237 5 return;
3238 }
3239 12709 str->append_ulonglong(value);
3240 }
3241
3242 254314 Item_decimal::Item_decimal(const POS &pos, const char *str_arg, uint length,
3243 254314 const CHARSET_INFO *charset)
3244 254314 : super(pos) {
3245
1/2
✓ Branch 0 taken 254316 times.
✗ Branch 1 not taken.
254322 str2my_decimal(E_DEC_FATAL_ERROR, str_arg, length, charset, &decimal_value);
3246 254316 item_name.set(str_arg);
3247 254326 set_data_type(MYSQL_TYPE_NEWDECIMAL);
3248 254324 decimals = (uint8)decimal_value.frac;
3249 254324 fixed = true;
3250 508644 max_length = my_decimal_precision_to_length_no_truncation(
3251 254324 decimal_value.intg + decimals, decimals, unsigned_flag);
3252 254320 }
3253
3254 173 Item_decimal::Item_decimal(longlong val, bool unsig) {
3255
1/2
✓ Branch 0 taken 173 times.
✗ Branch 1 not taken.
173 int2my_decimal(E_DEC_FATAL_ERROR, val, unsig, &decimal_value);
3256 173 set_data_type(MYSQL_TYPE_NEWDECIMAL);
3257 173 decimals = (uint8)decimal_value.frac;
3258 173 fixed = true;
3259 346 max_length = my_decimal_precision_to_length_no_truncation(
3260 173 decimal_value.intg + decimals, decimals, unsigned_flag);
3261 173 }
3262
3263 1 Item_decimal::Item_decimal(double val) {
3264
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 double2my_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
3265 1 set_data_type(MYSQL_TYPE_NEWDECIMAL);
3266 1 decimals = (uint8)decimal_value.frac;
3267 1 fixed = true;
3268 2 max_length = my_decimal_precision_to_length_no_truncation(
3269 1 decimal_value.intg + decimals, decimals, unsigned_flag);
3270 1 }
3271
3272 Item_decimal::Item_decimal(const Name_string &name_arg,
3273 const my_decimal *val_arg, uint decimal_par,
3274 uint length) {
3275 my_decimal2decimal(val_arg, &decimal_value);
3276 item_name = name_arg;
3277 set_data_type(MYSQL_TYPE_NEWDECIMAL);
3278 decimals = (uint8)decimal_par;
3279 max_length = length;
3280 fixed = true;
3281 }
3282
3283 2016 Item_decimal::Item_decimal(my_decimal *value_par) {
3284
1/2
✓ Branch 0 taken 2016 times.
✗ Branch 1 not taken.
2016 my_decimal2decimal(value_par, &decimal_value);
3285 2016 set_data_type(MYSQL_TYPE_NEWDECIMAL);
3286 2016 decimals = (uint8)decimal_value.frac;
3287 2016 fixed = true;
3288 4032 max_length = my_decimal_precision_to_length_no_truncation(
3289 2016 decimal_value.intg + decimals, decimals, unsigned_flag);
3290 2016 }
3291
3292 7 Item_decimal::Item_decimal(const uchar *bin, int precision, int scale) {
3293
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 binary2my_decimal(E_DEC_FATAL_ERROR, bin, &decimal_value, precision, scale);
3294 7 set_data_type(MYSQL_TYPE_NEWDECIMAL);
3295 7 decimals = (uint8)decimal_value.frac;
3296 7 fixed = true;
3297 14 max_length = my_decimal_precision_to_length_no_truncation(precision, decimals,
3298 7 unsigned_flag);
3299 7 }
3300
3301 127 longlong Item_decimal::val_int() {
3302 longlong result;
3303
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &result);
3304 127 return result;
3305 }
3306
3307 18114 double Item_decimal::val_real() {
3308 double result;
3309
1/2
✓ Branch 0 taken 18114 times.
✗ Branch 1 not taken.
18114 my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
3310 18114 return result;
3311 }
3312
3313 222 String *Item_decimal::val_str(String *result) {
3314 222 result->set_charset(&my_charset_numeric);
3315 222 my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, result);
3316 222 return result;
3317 }
3318
3319 13711 void Item_decimal::print(const THD *, String *str,
3320 enum_query_type query_type) const {
3321
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 13696 times.
13711 if (query_type & QT_NORMALIZED_FORMAT) {
3322
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 str->append("?");
3323 15 return;
3324 }
3325 13696 StringBuffer<MAX_DOUBLE_STR_LENGTH + 1> tmp; // +1 for terminating null
3326
1/2
✓ Branch 0 taken 13696 times.
✗ Branch 1 not taken.
13696 my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, &tmp);
3327
1/2
✓ Branch 0 taken 13696 times.
✗ Branch 1 not taken.
13696 str->append(tmp);
3328 13696 }
3329
3330 1370 bool Item_decimal::eq(const Item *item, bool) const {
3331
5/6
✓ Branch 0 taken 1090 times.
✓ Branch 1 taken 280 times.
✓ Branch 2 taken 1090 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1090 times.
✓ Branch 5 taken 280 times.
1370 if (type() == item->type() && item->basic_const_item()) {
3332 /*
3333 We need to cast off const to call val_decimal(). This should
3334 be OK for a basic constant. Additionally, we can pass nullptr as
3335 a true decimal constant will return its internal decimal
3336 storage and ignore the argument.
3337 */
3338 1090 Item *arg = const_cast<Item *>(item);
3339 1090 const my_decimal *value = arg->val_decimal(nullptr);
3340 1090 return !my_decimal_cmp(&decimal_value, value);
3341 }
3342 280 return false;
3343 }
3344
3345 100 void Item_decimal::set_decimal_value(const my_decimal *value_par) {
3346 100 my_decimal2decimal(value_par, &decimal_value);
3347 100 decimals = (uint8)decimal_value.frac;
3348 100 unsigned_flag = !decimal_value.sign();
3349 200 max_length = my_decimal_precision_to_length_no_truncation(
3350 100 decimal_value.intg + decimals, decimals, unsigned_flag);
3351 100 }
3352
3353 33 String *Item_float::val_str(String *str) {
3354 // following assert is redundant, because fixed=1 assigned in constructor
3355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 assert(fixed == 1);
3356 33 str->set_real(value, decimals, &my_charset_bin);
3357 33 return str;
3358 }
3359
3360 117 my_decimal *Item_float::val_decimal(my_decimal *decimal_value) {
3361 // following assert is redundant, because fixed=1 assigned in constructor
3362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 117 times.
117 assert(fixed == 1);
3363 117 double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_value);
3364 117 return (decimal_value);
3365 }
3366
3367 381 bool Item_string::set_str_with_copy(const char *str_arg, uint length_arg,
3368 const CHARSET_INFO *from_cs) {
3369 unsigned errors;
3370
2/4
✓ Branch 0 taken 381 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 381 times.
381 if (str_value.copy(str_arg, length_arg, from_cs, collation.collation,
3371 &errors)) {
3372 return true;
3373 }
3374
3375 381 fix_char_length(str_value.length());
3376 381 return false;
3377 }
3378
3379 /**
3380 @sa enum_query_type.
3381 For us to be able to print a query (in debugging, optimizer trace, EXPLAIN
3382 EXTENDED) without changing the query's result, this function must not
3383 modify the item's content. Not even a @c realloc() of @c str_value is
3384 permitted:
3385 @c Item_func_concat::val_str(), @c Item_func_repeat::val_str(),
3386 @c Item_func_encode::val_str() depend on the allocated length;
3387 a change of this length can influence results of CONCAT(), REPEAT(),
3388 ENCODE()...
3389 */
3390 1189487 void Item_string::print(const THD *, String *str,
3391 enum_query_type query_type) const {
3392
2/2
✓ Branch 0 taken 698 times.
✓ Branch 1 taken 1188789 times.
1189487 if (query_type & QT_NORMALIZED_FORMAT) {
3393
1/2
✓ Branch 0 taken 698 times.
✗ Branch 1 not taken.
698 str->append("?");
3394 698 return;
3395 }
3396
3397 const bool print_introducer =
3398
2/2
✓ Branch 0 taken 1169963 times.
✓ Branch 1 taken 18826 times.
2358752 (query_type & QT_FORCE_INTRODUCERS) ||
3399
4/4
✓ Branch 0 taken 872221 times.
✓ Branch 1 taken 297742 times.
✓ Branch 2 taken 333 times.
✓ Branch 3 taken 871888 times.
1169963 (!(query_type & QT_WITHOUT_INTRODUCERS) && is_cs_specified());
3400
3401
2/2
✓ Branch 0 taken 19159 times.
✓ Branch 1 taken 1169630 times.
1188789 if (print_introducer) {
3402 19159 str->append('_');
3403
1/2
✓ Branch 0 taken 19159 times.
✗ Branch 1 not taken.
19159 str->append(collation.collation->csname);
3404 }
3405
3406 1188789 str->append('\'');
3407
3408
2/2
✓ Branch 0 taken 306733 times.
✓ Branch 1 taken 882056 times.
1188789 if (query_type & QT_TO_SYSTEM_CHARSET) {
3409
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 306568 times.
306733 if (print_introducer) {
3410 /*
3411 Because we wrote an introducer, we must print str_value in its
3412 charset, and the resulting bytes must not be changed until they
3413 reach the end client.
3414 But the caller is asking for system_charset_info, and may later
3415 convert into character_set_results. That means two conversions: we
3416 must ensure that they don't change our printed bytes.
3417 So we print str_value in the least common denominator of the three
3418 charsets involved: ASCII. Non-ASCII characters are printed as \xFF
3419 sequences (which is ASCII too). This way, our bytes will not be
3420 changed.
3421 */
3422
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 ErrConvString tmp(str_value.ptr(), str_value.length(), &my_charset_bin);
3423
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 str->append(tmp.ptr());
3424 } else {
3425 // Convert to system charset.
3426 306568 convert_and_print(&str_value, str, system_charset_info);
3427 }
3428
2/2
✓ Branch 0 taken 856398 times.
✓ Branch 1 taken 25658 times.
882056 } else if (query_type & QT_TO_ARGUMENT_CHARSET) {
3429
2/2
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 856283 times.
856398 if (print_introducer)
3430 115 convert_and_print(&str_value, str, collation.collation);
3431 else
3432 /*
3433 Convert the string literals to str->charset(),
3434 which is typically equal to charset_set_client.
3435 */
3436 856283 convert_and_print(&str_value, str, str->charset());
3437 } else {
3438 // Caller wants a result in the charset of str_value.
3439 25658 str_value.print(str);
3440 }
3441
3442 1188789 str->append('\'');
3443 }
3444
3445 63299 double double_from_string_with_check(const CHARSET_INFO *cs, const char *cptr,
3446 const char *end) {
3447 int error;
3448 double tmp;
3449
3450 63299 const char *endptr = end;
3451
1/2
✓ Branch 0 taken 63299 times.
✗ Branch 1 not taken.
63299 tmp = my_strntod(cs, cptr, end - cptr, &endptr, &error);
3452
9/10
✓ Branch 0 taken 63298 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5516 times.
✓ Branch 3 taken 57782 times.
✓ Branch 4 taken 5516 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5514 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 5515 times.
✓ Branch 9 taken 57784 times.
63299 if (error || (end != endptr && !check_if_only_end_space(cs, endptr, end))) {
3453
1/2
✓ Branch 0 taken 5515 times.
✗ Branch 1 not taken.
5515 ErrConvString err(cptr, end - cptr, cs);
3454 5515 push_warning_printf(
3455
2/4
✓ Branch 0 taken 5515 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5515 times.
✗ Branch 3 not taken.
5515 current_thd, Sql_condition::SL_WARNING, ER_TRUNCATED_WRONG_VALUE,
3456
2/4
✓ Branch 0 taken 5515 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5515 times.
✗ Branch 3 not taken.
5515 ER_THD(current_thd, ER_TRUNCATED_WRONG_VALUE), "DOUBLE", err.ptr());
3457 }
3458 63299 return tmp;
3459 }
3460
3461 21563 double Item_string::val_real() {
3462
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21563 times.
21563 assert(fixed == 1);
3463 21563 return double_from_string_with_check(str_value.charset(), str_value.ptr(),
3464 43126 str_value.ptr() + str_value.length());
3465 }
3466
3467 /**
3468 Converts a string to a longlong integer, with warnings.
3469
3470 @param cs charset of string
3471 @param cptr beginning of string
3472 @param end end of string
3473 @param unsigned_target If 0, caller will use result as a signed integer;
3474 if 1: an unsigned integer;
3475 if -1: caller doesn't tell. This influences warnings.
3476 */
3477 201498 longlong longlong_from_string_with_check(const CHARSET_INFO *cs,
3478 const char *cptr, const char *end,
3479 int unsigned_target) {
3480 int err;
3481 longlong tmp;
3482 201498 const char *endptr = end;
3483
3484
1/2
✓ Branch 0 taken 201498 times.
✗ Branch 1 not taken.
201498 tmp = (*(cs->cset->strtoll10))(cs, cptr, &endptr, &err);
3485
4/4
✓ Branch 0 taken 201110 times.
✓ Branch 1 taken 388 times.
✓ Branch 2 taken 562 times.
✓ Branch 3 taken 200936 times.
402608 if (err > 0 || // range error, or
3486 // parse error not due to end spaces:
3487
5/6
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 200932 times.
✓ Branch 2 taken 178 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 174 times.
✓ Branch 5 taken 4 times.
201110 (end != endptr && !check_if_only_end_space(cs, endptr, end))) {
3488
1/2
✓ Branch 0 taken 562 times.
✗ Branch 1 not taken.
562 ErrConvString errstr(cptr, end - cptr, cs);
3489
3490 562 push_warning_printf(
3491
2/4
✓ Branch 0 taken 562 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 562 times.
✗ Branch 3 not taken.
562 current_thd, Sql_condition::SL_WARNING, ER_TRUNCATED_WRONG_VALUE,
3492
2/4
✓ Branch 0 taken 562 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 562 times.
✗ Branch 3 not taken.
562 ER_THD(current_thd, ER_TRUNCATED_WRONG_VALUE), "INTEGER", errstr.ptr());
3493 }
3494
4/4
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 201399 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 74 times.
201498 if (err < 0 && // string has a minus sign.
3495 unsigned_target == 1) // value will be used as unsigned.
3496
2/4
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
25 push_warning(current_thd, Sql_condition::SL_WARNING, ER_UNKNOWN_ERROR,
3497 "Cast to unsigned converted negative integer to its "
3498 "positive complement");
3499
4/4
✓ Branch 0 taken 201011 times.
✓ Branch 1 taken 462 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 200995 times.
201473 else if (err == 0 && // string had no minus sign
3500
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 10 times.
16 tmp < 0 && // the unsigned value is greater than max signed int
3501 unsigned_target == 0) // and will be used as signed.
3502 {
3503
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 push_warning(current_thd, Sql_condition::SL_WARNING, ER_UNKNOWN_ERROR,
3504 "Cast to signed converted positive out-of-range integer to "
3505 "its negative complement");
3506 }
3507 201498 return tmp;
3508 }
3509
3510 504 longlong Item_string::val_int() {
3511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 504 times.
504 assert(fixed);
3512 504 return longlong_from_string_with_check(str_value.charset(), str_value.ptr(),
3513 504 str_value.ptr() + str_value.length(),
3514 504 -1); // ignore sign issues
3515 }
3516
3517 517 my_decimal *Item_string::val_decimal(my_decimal *decimal_value) {
3518 517 return val_decimal_from_string(decimal_value);
3519 }
3520
3521 956 bool Item_null::eq(const Item *item, bool) const {
3522 956 return item->type() == type();
3523 }
3524
3525 949 double Item_null::val_real() {
3526 // following assert is redundant, because fixed=1 assigned in constructor
3527
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 949 times.
949 assert(fixed == 1);
3528 949 null_value = true;
3529 949 return 0.0;
3530 }
3531 5659797 longlong Item_null::val_int() {
3532 // following assert is redundant, because fixed=1 assigned in constructor
3533
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5659797 times.
5659797 assert(fixed == 1);
3534 5659797 null_value = true;
3535 5659797 return 0;
3536 }
3537
3538 6931201 String *Item_null::val_str(String *) {
3539 // following assert is redundant, because fixed=1 assigned in constructor
3540
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6931201 times.
6931201 assert(fixed == 1);
3541 6931201 null_value = true;
3542 6931201 return nullptr;
3543 }
3544
3545 310 my_decimal *Item_null::val_decimal(my_decimal *) { return nullptr; }
3546
3547 bool Item_null::val_json(Json_wrapper *) {
3548 null_value = true;
3549 return false;
3550 }
3551
3552 28 Item *Item_null::safe_charset_converter(THD *, const CHARSET_INFO *tocs) {
3553 28 collation.set(tocs);
3554 28 return this;
3555 }
3556
3557 /*********************** Item_param related ******************************/
3558
3559 284850 Item_param::Item_param(const POS &pos, MEM_ROOT *root, uint pos_in_query_arg)
3560
1/2
✓ Branch 0 taken 284850 times.
✗ Branch 1 not taken.
284850 : super(pos), pos_in_query(pos_in_query_arg), m_clones(root) {
3561 284850 item_name.set("?");
3562 // Initial type is "invalid type", type will be assigned from context
3563 284850 set_nullable(true); // All parameters are nullable
3564 284850 }
3565
3566 284830 bool Item_param::itemize(Parse_context *pc, Item **res) {
3567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 284833 times.
284830 if (skip_itemize(res)) return false;
3568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 284833 times.
284833 if (super::itemize(pc, res)) return true;
3569
3570 /*
3571 see commentaries in PTI_limit_option_param_marker::itemize()
3572 */
3573
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 284833 times.
284833 assert(*res == this);
3574
3575 284833 LEX *lex = pc->thd->lex;
3576
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 284832 times.
284833 if (!lex->parsing_options.allows_variable) {
3577 1 my_error(ER_VIEW_SELECT_VARIABLE, MYF(0));
3578 1 return true;
3579 }
3580
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 284824 times.
284832 if (lex->reparse_common_table_expr_at) {
3581 /*
3582 This parameter is a clone, find the Item_param which corresponds to it
3583 in the original statement - its "master".
3584 Calculate the expected position of this master in the original
3585 statement:
3586 */
3587 8 uint master_pos = pos_in_query + lex->reparse_common_table_expr_at;
3588
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 List_iterator_fast<Item_param> it(lex->param_list);
3589 Item_param *master;
3590
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 while ((master = it++)) {
3591
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (master_pos == master->pos_in_query) {
3592 // Register it against its master
3593
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 return master->add_clone(this);
3594 }
3595 }
3596 assert(false); /* purecov: inspected */
3597 }
3598
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 284784 times.
284824 if (!lex->reparse_derived_table_params_at.empty()) {
3599 // This parameter is a clone, find the Item_param which corresponds
3600 // to it in the original statement - its "master".
3601
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 List_iterator_fast<Item_param> it(lex->param_list);
3602 Item_param *master;
3603 38 auto master_pos = lex->reparse_derived_table_params_at.begin();
3604
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
98 while ((master = it++)) {
3605
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 60 times.
98 if (*master_pos == master->pos_in_query) {
3606
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 lex->reparse_derived_table_params_at.erase(master_pos);
3607 // Register it against its master
3608 38 pos_in_query = master->pos_in_query;
3609
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 return master->add_clone(this);
3610 }
3611 }
3612 assert(false);
3613 }
3614
3615 284784 return false;
3616 }
3617
3618 152891 bool Item_param::fix_fields(THD *, Item **) {
3619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 152891 times.
152891 assert(!fixed);
3620
2/2
✓ Branch 0 taken 146592 times.
✓ Branch 1 taken 6299 times.
152891 if (param_state() == NO_VALUE) {
3621 // Parameter has no value, set data type from context
3622
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 146592 times.
146592 assert(data_type() == MYSQL_TYPE_INVALID);
3623 146592 fixed = true;
3624 146592 return false;
3625 }
3626
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 6284 times.
6299 if (param_state() == NULL_VALUE) {
3627 // Parameter data type may be ignored, keep existing type
3628 15 fixed = true;
3629 15 return false;
3630 }
3631 // Assign data type from actual data value, when given
3632
7/8
✓ Branch 0 taken 1993 times.
✓ Branch 1 taken 2434 times.
✓ Branch 2 taken 1507 times.
✓ Branch 3 taken 174 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 8 times.
✓ Branch 6 taken 163 times.
✗ Branch 7 not taken.
6284 switch (data_type_actual()) {
3633 1993 case MYSQL_TYPE_LONGLONG:
3634 1993 set_data_type_longlong();
3635 1993 unsigned_flag = is_unsigned_actual();
3636 1993 break;
3637 2434 case MYSQL_TYPE_NEWDECIMAL:
3638 2434 set_data_type_decimal(DECIMAL_MAX_PRECISION, DECIMAL_MAX_SCALE);
3639 2434 break;
3640 1507 case MYSQL_TYPE_DOUBLE:
3641 1507 set_data_type_double();
3642 1507 break;
3643 174 case MYSQL_TYPE_VARCHAR:
3644 // Set data type string with maximum possible size
3645 // @todo WL#6570 - what about blob values???
3646 174 set_data_type_string(65535U / m_collation_actual->mbmaxlen,
3647 m_collation_actual);
3648 174 break;
3649 5 case MYSQL_TYPE_DATE:
3650 5 set_data_type_date();
3651 5 break;
3652 8 case MYSQL_TYPE_TIME:
3653 8 set_data_type_time(DATETIME_MAX_DECIMALS);
3654 8 break;
3655 163 case MYSQL_TYPE_DATETIME:
3656 163 set_data_type_datetime(DATETIME_MAX_DECIMALS);
3657 163 break;
3658 default:
3659 assert(false);
3660 }
3661 // Do not set result type until having a valid type type (i.e. keep original)
3662
1/2
✓ Branch 0 taken 6284 times.
✗ Branch 1 not taken.
6284 if (data_type() != MYSQL_TYPE_INVALID)
3663 6284 m_result_type = type_to_result(data_type());
3664
3665 6284 fixed = true;
3666
3667 6284 return false;
3668 }
3669
3670 146603 bool Item_param::propagate_type(THD *, const Type_properties &type) {
3671
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 146603 times.
146603 assert(type.m_type != MYSQL_TYPE_INVALID);
3672
13/14
✓ Branch 0 taken 9159 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 102 times.
✓ Branch 3 taken 780 times.
✓ Branch 4 taken 132083 times.
✓ Branch 5 taken 2322 times.
✓ Branch 6 taken 131 times.
✓ Branch 7 taken 157 times.
✓ Branch 8 taken 872 times.
✓ Branch 9 taken 528 times.
✓ Branch 10 taken 213 times.
✓ Branch 11 taken 254 times.
✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
146603 switch (type.m_type) {
3673 9159 case MYSQL_TYPE_TINY:
3674 case MYSQL_TYPE_SHORT:
3675 case MYSQL_TYPE_INT24:
3676 case MYSQL_TYPE_LONG:
3677 case MYSQL_TYPE_LONGLONG:
3678 9159 set_data_type_longlong();
3679 9159 unsigned_flag = type.m_unsigned_flag;
3680 9159 break;
3681 1 case MYSQL_TYPE_BIT:
3682 1 set_data_type_bit();
3683 1 break;
3684 102 case MYSQL_TYPE_YEAR:
3685 102 set_data_type_year();
3686 102 break;
3687 780 case MYSQL_TYPE_NEWDECIMAL:
3688 case MYSQL_TYPE_DECIMAL:
3689 780 set_data_type_decimal(DECIMAL_MAX_PRECISION, DECIMAL_MAX_SCALE);
3690 780 break;
3691 132083 case MYSQL_TYPE_FLOAT:
3692 case MYSQL_TYPE_DOUBLE:
3693 132083 set_data_type_double();
3694 132083 break;
3695 2322 case MYSQL_TYPE_VARCHAR:
3696 case MYSQL_TYPE_VAR_STRING:
3697 case MYSQL_TYPE_STRING:
3698 case MYSQL_TYPE_ENUM:
3699 case MYSQL_TYPE_SET:
3700 // Parameter type is VARCHAR of largest possible size
3701 2322 set_data_type_string(65535U / type.m_collation.collation->mbmaxlen,
3702 2322 type.m_collation);
3703 2322 break;
3704 131 case MYSQL_TYPE_GEOMETRY:
3705 131 set_data_type_geometry();
3706 131 break;
3707 157 case MYSQL_TYPE_JSON:
3708 157 set_data_type_json();
3709 157 break;
3710 872 case MYSQL_TYPE_TINY_BLOB:
3711 case MYSQL_TYPE_MEDIUM_BLOB:
3712 case MYSQL_TYPE_LONG_BLOB:
3713 case MYSQL_TYPE_BLOB:
3714 // Parameter type is BLOB of largest possible size
3715 872 set_data_type_string(Field::MAX_LONG_BLOB_WIDTH, type.m_collation);
3716 872 break;
3717 528 case MYSQL_TYPE_DATETIME:
3718 case MYSQL_TYPE_DATETIME2:
3719 case MYSQL_TYPE_TIMESTAMP:
3720 case MYSQL_TYPE_TIMESTAMP2:
3721 528 set_data_type_datetime(6);
3722 528 break;
3723 213 case MYSQL_TYPE_DATE:
3724 case MYSQL_TYPE_NEWDATE:
3725 213 set_data_type_date();
3726 213 break;
3727 254 case MYSQL_TYPE_TIME:
3728 case MYSQL_TYPE_TIME2:
3729 254 set_data_type_time(6);
3730 254 break;
3731 1 case MYSQL_TYPE_NULL:
3732 1 set_data_type_string(65535U / type.m_collation.collation->mbmaxlen,
3733 1 type.m_collation);
3734 1 break;
3735 default:
3736 assert(false);
3737 }
3738
3739 146603 m_result_type = type_to_result(data_type());
3740
3741 146603 return false;
3742 }
3743
3744 454608 void Item_param::sync_clones() {
3745
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 454632 times.
454693 for (auto c : m_clones) {
3746 // Scalar-type members:
3747 92 c->set_nullable(is_nullable());
3748 92 c->null_value = null_value;
3749 92 c->max_length = max_length;
3750 92 c->decimals = decimals;
3751 92 c->unsigned_flag = unsigned_flag;
3752 92 c->m_param_state = m_param_state;
3753 92 c->m_result_type = m_result_type;
3754 92 c->value = value;
3755 92 c->m_data_type_source = m_data_type_source;
3756 92 c->m_data_type_actual = m_data_type_actual;
3757 92 c->m_unsigned_actual = m_unsigned_actual;
3758 92 c->m_collation_source = m_collation_source;
3759 92 c->m_collation_actual = m_collation_actual;
3760 // Class-type members:
3761 92 c->decimal_value = decimal_value;
3762 /*
3763 Note that String's assignment op properly sets m_is_alloced to 'false',
3764 which is correct here: c->str_value doesn't own anything.
3765 */
3766 92 c->str_value = str_value;
3767 92 c->str_value_ptr = str_value_ptr;
3768 85 c->collation = collation;
3769 }
3770 454632 }
3771
3772 1117 void Item_param::set_null() {
3773
1/2
✓ Branch 0 taken 1117 times.
✗ Branch 1 not taken.
1117 DBUG_TRACE;
3774
3775 1117 null_value = true;
3776
3777 1117 m_data_type_actual = MYSQL_TYPE_NULL;
3778 1117 m_param_state = NULL_VALUE;
3779 1117 }
3780
3781 9763 void Item_param::set_int(longlong i) {
3782
1/2
✓ Branch 0 taken 9781 times.
✗ Branch 1 not taken.
9763 DBUG_TRACE;
3783 9781 value.integer = i;
3784 9781 m_data_type_actual = MYSQL_TYPE_LONGLONG;
3785 9781 m_unsigned_actual = false;
3786 9781 m_param_state = INT_VALUE;
3787 9781 }
3788
3789 220 void Item_param::set_int(ulonglong i) {
3790
1/2
✓ Branch 0 taken 220 times.
✗ Branch 1 not taken.
220 DBUG_TRACE;
3791 220 value.integer = i;
3792 220 m_data_type_actual = MYSQL_TYPE_LONGLONG;
3793 220 m_unsigned_actual = true;
3794 220 m_param_state = INT_VALUE;
3795 220 }
3796
3797 133198 void Item_param::set_double(double d) {
3798
1/2
✓ Branch 0 taken 133198 times.
✗ Branch 1 not taken.
133198 DBUG_TRACE;
3799 133198 value.real = d;
3800 133198 m_data_type_actual = MYSQL_TYPE_DOUBLE;
3801 133198 m_param_state = REAL_VALUE;
3802 133198 }
3803
3804 /**
3805 Set decimal parameter value from string.
3806
3807 @param str character string
3808 @param length string length
3809
3810 @note
3811 As we use character strings to send decimal values in
3812 binary protocol, we use str2my_decimal to convert it to
3813 internal decimal value.
3814 */
3815
3816 14 void Item_param::set_decimal(const char *str, ulong length) {
3817
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 DBUG_TRACE;
3818
3819 14 const char *end = str + length;
3820
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 str2my_decimal(E_DEC_FATAL_ERROR, str, &decimal_value, &end);
3821 14 m_data_type_actual = MYSQL_TYPE_NEWDECIMAL;
3822 14 m_param_state = DECIMAL_VALUE;
3823 14 }
3824
3825 20 void Item_param::set_decimal(const my_decimal *dv) {
3826 20 m_param_state = DECIMAL_VALUE;
3827 20 m_data_type_actual = MYSQL_TYPE_NEWDECIMAL;
3828
3829 20 my_decimal2decimal(dv, &decimal_value);
3830 20 }
3831
3832 /**
3833 Set parameter value from MYSQL_TIME value.
3834
3835 @param tm datetime value to set (time_type is ignored)
3836 @param time_type type of datetime value
3837
3838 @note
3839 If we value to be stored is not normalized, zero value will be stored
3840 instead and proper warning will be produced. This function relies on
3841 the fact that even wrong value sent over binary protocol fits into
3842 MAX_DATE_STRING_REP_LENGTH buffer.
3843 */
3844 221 void Item_param::set_time(MYSQL_TIME *tm, enum_mysql_timestamp_type time_type) {
3845
1/2
✓ Branch 0 taken 221 times.
✗ Branch 1 not taken.
221 DBUG_TRACE;
3846
3847
7/8
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 124 times.
✓ Branch 3 taken 42 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 122 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
221 assert(time_type == MYSQL_TIMESTAMP_DATE ||
3848 time_type == MYSQL_TIMESTAMP_TIME ||
3849 time_type == MYSQL_TIMESTAMP_DATETIME ||
3850 time_type == MYSQL_TIMESTAMP_DATETIME_TZ);
3851
3852 221 value.time = *tm;
3853 221 value.time.time_type = time_type;
3854
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 139 times.
221 decimals = tm->second_part ? DATETIME_MAX_DECIMALS : 0;
3855
3856
3/4
✓ Branch 0 taken 221 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 209 times.
221 if (check_datetime_range(value.time)) {
3857 /*
3858 TODO : Add error handling for Item_param::set_* functions.
3859 make_truncated_value_warning() can return error in STRICT mode.
3860 */
3861
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 (void)make_truncated_value_warning(current_thd, Sql_condition::SL_WARNING,
3862
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 ErrConvString(&value.time, decimals),
3863 time_type, NullS);
3864
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 set_zero_time(&value.time, MYSQL_TIMESTAMP_ERROR);
3865 }
3866
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 166 times.
221 if (time_type == MYSQL_TIMESTAMP_DATE)
3867 55 m_data_type_actual = MYSQL_TYPE_DATE;
3868
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 124 times.
166 else if (time_type == MYSQL_TIMESTAMP_TIME)
3869 42 m_data_type_actual = MYSQL_TYPE_TIME;
3870 else
3871 124 m_data_type_actual = MYSQL_TYPE_DATETIME;
3872
3873 221 m_param_state = TIME_VALUE;
3874 221 }
3875
3876 8201 bool Item_param::set_str(const char *str, size_t length) {
3877
1/2
✓ Branch 0 taken 8225 times.
✗ Branch 1 not taken.
8201 DBUG_TRACE;
3878 /*
3879 Assign string with no conversion: data is converted only after it's
3880 been written to the binary log.
3881 */
3882 uint dummy_errors;
3883
2/4
✓ Branch 0 taken 8222 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8222 times.
8225 if (str_value.copy(str, length, &my_charset_bin, &my_charset_bin,
3884 &dummy_errors))
3885 return true;
3886 8222 m_data_type_actual = MYSQL_TYPE_VARCHAR;
3887 /*
3888 Generally, the character set of the string stored in the parameter object
3889 is the resolved character set of the parameter, except:
3890 - when the resolved character set is a binary string, ensure the string
3891 is in the connection character set.
3892 - when the source string is a binary string, keep it as-is and perform
3893 no conversion.
3894 */
3895
2/2
✓ Branch 0 taken 7734 times.
✓ Branch 1 taken 490 times.
15956 set_collation_actual(collation_source() == &my_charset_bin
3896 ? &my_charset_bin
3897 7734 : collation.collation != &my_charset_bin
3898
2/2
✓ Branch 0 taken 7580 times.
✓ Branch 1 taken 154 times.
7734 ? collation.collation
3899
1/2
✓ Branch 0 taken 154 times.
✗ Branch 1 not taken.
154 : current_thd->variables.collation_connection);
3900
3901 8226 m_param_state = STRING_VALUE;
3902 8226 return false;
3903 8226 }
3904
3905 492 bool Item_param::set_longdata(const char *str, ulong length) {
3906
1/2
✓ Branch 0 taken 492 times.
✗ Branch 1 not taken.
492 DBUG_TRACE;
3907
3908 /*
3909 If client character set is multibyte, end of long data packet
3910 may hit at the middle of a multibyte character. Additionally,
3911 if binary log is open we must write long data value to the
3912 binary log in character set of client. This is why we can't
3913 convert long data to connection character set as it comes
3914 (here), and first have to concatenate all pieces together,
3915 write query to the binary log and only then perform conversion.
3916 */
3917
3/4
✓ Branch 0 taken 492 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 488 times.
492 if (str_value.length() + length > current_thd->variables.max_allowed_packet) {
3918
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 my_message(ER_UNKNOWN_ERROR,
3919 "Parameter of prepared statement which is set through "
3920 "mysql_send_long_data() is longer than "
3921 "'max_allowed_packet' bytes",
3922 MYF(0));
3923 4 return true;
3924 }
3925
3926
2/4
✓ Branch 0 taken 488 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 488 times.
488 if (str_value.append(str, length, &my_charset_bin)) return true;
3927
3928 /*
3929 Currently, both source type and actual type is MYSQL_TYPE_INVALID.
3930 They will be set to proper values by Prepared_statement::insert_params().
3931 */
3932 488 m_param_state = LONG_DATA_VALUE;
3933
3934 488 return false;
3935 492 }
3936
3937 /**
3938 Set parameter value from user variable value.
3939
3940 @param thd Current thread
3941 @param entry User variable structure (NULL means use NULL value)
3942
3943 @returns false if success, true if error
3944 */
3945
3946 20283 bool Item_param::set_from_user_var(THD *thd [[maybe_unused]],
3947 const user_var_entry *entry) {
3948
1/2
✓ Branch 0 taken 20320 times.
✗ Branch 1 not taken.
20283 DBUG_TRACE;
3949
6/6
✓ Branch 0 taken 20282 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 19267 times.
✓ Branch 3 taken 1014 times.
✓ Branch 4 taken 19265 times.
✓ Branch 5 taken 1054 times.
20320 if (entry && entry->ptr()) {
3950 // An existing user variable that is not NULL
3951
3952 // Pinning of data types only implemented for integers
3953
3/4
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 19176 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 71 times.
19265 assert(!is_type_pinned() || result_type() == INT_RESULT);
3954
6/6
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 19186 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 64 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 19250 times.
19247 if (is_type_pinned() && entry->type() != INT_RESULT) {
3955
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 my_error(ER_WRONG_ARGUMENTS, MYF(0), "EXECUTE");
3956 7 return true;
3957 }
3958
5/5
✓ Branch 0 taken 1885 times.
✓ Branch 1 taken 6878 times.
✓ Branch 2 taken 7556 times.
✓ Branch 3 taken 2895 times.
✓ Branch 4 taken 38 times.
19250 switch (entry->type()) {
3959 1885 case REAL_RESULT:
3960
1/2
✓ Branch 0 taken 1885 times.
✗ Branch 1 not taken.
1885 set_double(*pointer_cast<const double *>(entry->ptr()));
3961 1885 break;
3962 6878 case INT_RESULT:
3963
2/2
✓ Branch 0 taken 194 times.
✓ Branch 1 taken 6684 times.
6878 if (entry->unsigned_flag) {
3964 194 ulonglong val = *pointer_cast<const ulonglong *>(entry->ptr());
3965
4/8
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 189 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 194 times.
194 if (is_type_pinned() && !unsigned_flag && val > INT_MAX64) {
3966 my_error(ER_DATA_OUT_OF_RANGE, MYF(0), "signed integer", "EXECUTE");
3967 return true;
3968 }
3969
1/2
✓ Branch 0 taken 194 times.
✗ Branch 1 not taken.
194 set_int(val);
3970 } else {
3971 6684 longlong val = *pointer_cast<const longlong *>(entry->ptr());
3972
8/8
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 6630 times.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 21 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 35 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 6686 times.
6683 if (is_type_pinned() && unsigned_flag && val < 0) {
3973
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 my_error(ER_DATA_OUT_OF_RANGE, MYF(0), "unsigned integer",
3974 "EXECUTE");
3975 3 return true;
3976 }
3977
1/2
✓ Branch 0 taken 6702 times.
✗ Branch 1 not taken.
6686 set_int(val);
3978 }
3979 6896 break;
3980 7556 case STRING_RESULT:
3981
2/4
✓ Branch 0 taken 7583 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7583 times.
7556 if (set_str(entry->ptr(), entry->length())) return true;
3982 7583 break;
3983 2895 case DECIMAL_RESULT: {
3984 2895 const my_decimal *ent_value = (const my_decimal *)entry->ptr();
3985
1/2
✓ Branch 0 taken 2895 times.
✗ Branch 1 not taken.
2895 my_decimal2decimal(ent_value, &decimal_value);
3986 2895 m_data_type_actual = MYSQL_TYPE_NEWDECIMAL;
3987 2895 m_param_state = DECIMAL_VALUE;
3988 2895 break;
3989 }
3990 38 default:
3991 38 assert(0);
3992 set_null();
3993 }
3994 } else {
3995
1/2
✓ Branch 0 taken 1053 times.
✗ Branch 1 not taken.
1054 set_null();
3996 }
3997 20312 return false;
3998 20322 }
3999
4000 /**
4001 Resets parameter after execution.
4002
4003 @note
4004 We clear null_value here instead of setting it in set_* methods,
4005 because we want more easily handle case for long data.
4006 */
4007
4008 286515 void Item_param::reset() {
4009
1/2
✓ Branch 0 taken 286558 times.
✗ Branch 1 not taken.
286515 DBUG_TRACE;
4010 /* Shrink string buffer if it's bigger than max possible CHAR column */
4011
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 286541 times.
286558 if (str_value.alloced_length() > MAX_CHAR_WIDTH)
4012
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 str_value.mem_free();
4013 else
4014 286541 str_value.length(0);
4015 286566 str_value_ptr.length(0);
4016 286544 m_param_state = NO_VALUE;
4017 286544 m_data_type_actual = MYSQL_TYPE_INVALID;
4018 286544 null_value = false;
4019 286544 }
4020
4021 144007 type_conversion_status Item_param::save_in_field_inner(Field *field,
4022 bool no_conversions) {
4023
2/2
✓ Branch 0 taken 878 times.
✓ Branch 1 taken 143160 times.
144007 if (param_state() == NULL_VALUE) {
4024 878 return set_field_to_null_with_conversions(field, no_conversions);
4025 }
4026 143160 field->set_notnull();
4027
4028
5/6
✓ Branch 0 taken 6834 times.
✓ Branch 1 taken 131947 times.
✓ Branch 2 taken 390 times.
✓ Branch 3 taken 311 times.
✓ Branch 4 taken 3674 times.
✗ Branch 5 not taken.
143153 switch (data_type_actual()) {
4029 6834 case MYSQL_TYPE_LONGLONG:
4030 6834 return field->store(value.integer, is_unsigned_actual());
4031 131947 case MYSQL_TYPE_DOUBLE:
4032 131947 return field->store(value.real);
4033 390 case MYSQL_TYPE_NEWDECIMAL:
4034 390 return field->store_decimal(&decimal_value);
4035 311 case MYSQL_TYPE_DATE:
4036 case MYSQL_TYPE_TIME:
4037 case MYSQL_TYPE_DATETIME:
4038 311 field->store_time(&value.time);
4039 311 return TYPE_OK;
4040 3674 case MYSQL_TYPE_VARCHAR:
4041 3674 return field->store(str_value.ptr(), str_value.length(),
4042 3672 str_value.charset());
4043 default:
4044 assert(0);
4045 }
4046 return TYPE_ERR_BAD_VALUE;
4047 }
4048
4049 653 bool Item_param::get_time(MYSQL_TIME *res) {
4050
5/5
✓ Branch 0 taken 638 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 12 times.
653 switch (data_type_actual()) {
4051 638 case MYSQL_TYPE_TIME:
4052 case MYSQL_TYPE_DATE:
4053 case MYSQL_TYPE_DATETIME:
4054 638 *res = value.time;
4055 638 return false;
4056 1 case MYSQL_TYPE_LONGLONG:
4057 1 return get_time_from_int(res);
4058 1 case MYSQL_TYPE_DOUBLE:
4059 1 return get_time_from_real(res);
4060 1 case MYSQL_TYPE_NEWDECIMAL:
4061 1 return get_time_from_decimal(res);
4062 12 default:
4063 12 return get_time_from_string(res);
4064 }
4065 }
4066
4067 1054 bool Item_param::get_date(MYSQL_TIME *res, my_time_flags_t fuzzydate) {
4068
3/5
✓ Branch 0 taken 1019 times.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
1054 switch (data_type_actual()) {
4069 1019 case MYSQL_TYPE_TIME:
4070 case MYSQL_TYPE_DATE:
4071 case MYSQL_TYPE_DATETIME:
4072 1019 *res = value.time;
4073 1019 return false;
4074 5 case MYSQL_TYPE_LONGLONG:
4075 5 return get_date_from_int(res, fuzzydate);
4076 case MYSQL_TYPE_DOUBLE:
4077 return get_date_from_real(res, fuzzydate);
4078 case MYSQL_TYPE_NEWDECIMAL:
4079 return get_date_from_decimal(res, fuzzydate);
4080 30 default:
4081 30 return get_date_from_string(res, fuzzydate);
4082 }
4083 }
4084
4085 8099 double Item_param::val_real() {
4086
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8099 times.
8099 assert(data_type() != MYSQL_TYPE_INVALID);
4087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8099 times.
8099 assert(param_state() != NO_VALUE);
4088
4089
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8083 times.
8099 if (param_state() == NULL_VALUE) {
4090 16 return 0.0;
4091 }
4092
5/6
✓ Branch 0 taken 7318 times.
✓ Branch 1 taken 374 times.
✓ Branch 2 taken 378 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
8083 switch (data_type_actual()) {
4093 7318 case MYSQL_TYPE_DOUBLE:
4094 7318 return value.real;
4095 374 case MYSQL_TYPE_LONGLONG:
4096
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 373 times.
374 if (is_unsigned_actual())
4097 1 return static_cast<double>(static_cast<ulonglong>(value.integer));
4098 else
4099 373 return static_cast<double>(value.integer);
4100 378 case MYSQL_TYPE_NEWDECIMAL: {
4101 double result;
4102
1/2
✓ Branch 0 taken 378 times.
✗ Branch 1 not taken.
378 my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
4103 378 return result;
4104 }
4105 10 case MYSQL_TYPE_VARCHAR: {
4106 10 return double_from_string_with_check(
4107 10 str_value.charset(), str_value.ptr(),
4108 20 str_value.ptr() + str_value.length());
4109 }
4110 3 case MYSQL_TYPE_DATE:
4111 case MYSQL_TYPE_TIME:
4112 case MYSQL_TYPE_DATETIME:
4113 /*
4114 This works for example when user says SELECT ?+0.0 and supplies
4115 time value for the placeholder.
4116 */
4117 3 return TIME_to_double(value.time);
4118 default:
4119 assert(0);
4120 }
4121 return 0.0;
4122 }
4123
4124 22266 longlong Item_param::val_int() {
4125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22294 times.
22266 assert(data_type() != MYSQL_TYPE_INVALID);
4126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22294 times.
22294 assert(param_state() != NO_VALUE);
4127
4128
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 22156 times.
22294 if (param_state() == NULL_VALUE) {
4129 142 return 0;
4130 }
4131
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 22056 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 88 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 4 times.
22156 switch (data_type_actual()) {
4132 case MYSQL_TYPE_DOUBLE:
4133 return static_cast<longlong>(rint(value.real));
4134 22056 case MYSQL_TYPE_LONGLONG:
4135 22056 return value.integer;
4136 4 case MYSQL_TYPE_NEWDECIMAL: {
4137 longlong i;
4138
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &i);
4139 4 return i;
4140 }
4141 88 case MYSQL_TYPE_VARCHAR: {
4142 88 return longlong_from_string_with_check(
4143 88 str_value.charset(), str_value.ptr(),
4144 176 str_value.ptr() + str_value.length(), unsigned_flag);
4145 }
4146 2 case MYSQL_TYPE_DATE:
4147 case MYSQL_TYPE_TIME:
4148 case MYSQL_TYPE_DATETIME:
4149
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 return (longlong)propagate_datetime_overflow(current_thd, [&](int *w) {
4150 2 return TIME_to_ulonglong_round(value.time, w);
4151 2 });
4152 4 default:
4153 4 assert(0);
4154 }
4155 return 0;
4156 }
4157
4158 11687 my_decimal *Item_param::val_decimal(my_decimal *dec) {
4159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11687 times.
11687 assert(data_type() != MYSQL_TYPE_INVALID);
4160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11687 times.
11687 assert(param_state() != NO_VALUE);
4161
4162
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11684 times.
11687 if (param_state() == NULL_VALUE) {
4163 3 return nullptr;
4164 }
4165
4/6
✓ Branch 0 taken 11391 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 290 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
11684 switch (data_type_actual()) {
4166 11391 case MYSQL_TYPE_NEWDECIMAL:
4167 11391 return &decimal_value;
4168 case MYSQL_TYPE_DOUBLE:
4169 double2my_decimal(E_DEC_FATAL_ERROR, value.real, dec);
4170 return dec;
4171 290 case MYSQL_TYPE_LONGLONG:
4172 290 int2my_decimal(E_DEC_FATAL_ERROR, value.integer, is_unsigned_actual(),
4173 dec);
4174 290 return dec;
4175 1 case MYSQL_TYPE_VARCHAR:
4176 1 return val_decimal_from_string(dec);
4177 2 case MYSQL_TYPE_DATE:
4178 case MYSQL_TYPE_TIME:
4179 case MYSQL_TYPE_DATETIME:
4180 2 return date2my_decimal(&value.time, dec);
4181 default:
4182 assert(0);
4183 }
4184 return nullptr;
4185 }
4186
4187 3564 String *Item_param::val_str(String *str) {
4188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3564 times.
3564 assert(data_type() != MYSQL_TYPE_INVALID);
4189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3564 times.
3564 assert(param_state() != NO_VALUE);
4190
4191
2/2
✓ Branch 0 taken 155 times.
✓ Branch 1 taken 3409 times.
3564 if (param_state() == NULL_VALUE) {
4192 155 return nullptr;
4193 }
4194
4/6
✓ Branch 0 taken 3269 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 70 times.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 46 times.
✗ Branch 5 not taken.
3409 switch (data_type_actual()) {
4195 3269 case MYSQL_TYPE_VARCHAR:
4196 3269 return &str_value_ptr;
4197 case MYSQL_TYPE_DOUBLE:
4198 str->set_real(value.real, DECIMAL_NOT_SPECIFIED, &my_charset_bin);
4199 return str;
4200 70 case MYSQL_TYPE_LONGLONG:
4201 70 str->set_int(value.integer, is_unsigned_actual(), &my_charset_bin);
4202 70 return str;
4203 24 case MYSQL_TYPE_NEWDECIMAL:
4204
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, str) <= 1)
4205 24 return str;
4206 return nullptr;
4207 46 case MYSQL_TYPE_DATE:
4208 case MYSQL_TYPE_TIME:
4209 case MYSQL_TYPE_DATETIME: {
4210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (str->reserve(MAX_DATE_STRING_REP_LENGTH)) break;
4211
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 str->length(my_TIME_to_str(value.time, str->ptr(),
4212 46 min(decimals, uint8{DATETIME_MAX_DECIMALS})));
4213 46 str->set_charset(&my_charset_bin);
4214 46 return str;
4215 }
4216 default:
4217 assert(0);
4218 }
4219 return str;
4220 }
4221
4222 113 bool Item_param::val_json(Json_wrapper *wr) {
4223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
113 assert(fixed);
4224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
113 assert(data_type() != MYSQL_TYPE_INVALID);
4225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 113 times.
113 assert(param_state() != NO_VALUE);
4226
4227 113 String value;
4228 113 String tmp;
4229 226 return sql_scalar_to_json(this, "cast_as_json", &value, &tmp, wr, nullptr,
4230
1/2
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
226 m_json_as_scalar);
4231 113 }
4232
4233 6300 void Item_param::copy_param_actual_type(Item_param *from) {
4234 6300 set_data_type_source(from->data_type_source(), from->is_unsigned_actual());
4235 6300 set_data_type_actual(from->data_type_actual(), from->is_unsigned_actual());
4236 6300 m_collation_source = from->m_collation_source;
4237 6300 m_collation_actual = from->m_collation_actual;
4238 6300 m_param_state = from->m_param_state;
4239 /*
4240 In a repreparation, steps are:
4241 - parse, create new Item_param
4242 - copy_param_actual_type (sets m_param_state from old param, that makes it
4243 look like it has a value)
4244 - prepare_query()
4245 - swap_parameter_array() (sets value from old param).
4246 So, here the new Item_param is in a split-brain state.
4247 Thus in prepare_query() the optimizer tracing will try to print its value;
4248 so the not-yet-final value has to be reasonable; if we leave it random here
4249 we can crash (if using DECIMAL) (see query_val_str()).
4250 We do not copy any pointer-to-data (e.g. str_value), to have no problems
4251 with memory ownership.
4252 */
4253 6300 value = from->value;
4254
2/2
✓ Branch 0 taken 1833 times.
✓ Branch 1 taken 4467 times.
6300 switch (m_param_state) {
4255 1833 case DECIMAL_VALUE:
4256 // Propagate decimals' layout, and set number to zero
4257 1833 decimal_value.intg = from->decimal_value.intg;
4258 1833 decimal_value.frac = from->decimal_value.frac;
4259 1833 memset(decimal_value.buf, 0, DECIMAL_BUFF_LENGTH);
4260 1833 decimal_value.sign(from->decimal_value.sign());
4261 1833 break;
4262 // STRING_VALUE: str_value member was initialized by ctor already.
4263 4467 default:
4264 4467 break;
4265 }
4266 6300 }
4267
4268 /**
4269 Return Param item values in string format, for generating the dynamic
4270 query used in update/binary logs.
4271
4272 @param thd current thread
4273 @param[out] str String to fill with parameter
4274
4275 @returns supplied string on success, NULL on error
4276
4277 @todo
4278 - Change interface and implementation to fill log data in place
4279 and avoid one more memcpy/alloc between str and log string.
4280 - In case of error we need to notify replication
4281 that binary log contains wrong statement
4282 */
4283
4284 156014 const String *Item_param::query_val_str(const THD *thd, String *str) const {
4285
7/7
✓ Branch 0 taken 10079 times.
✓ Branch 1 taken 133333 times.
✓ Branch 2 taken 2994 times.
✓ Branch 3 taken 221 times.
✓ Branch 4 taken 8264 times.
✓ Branch 5 taken 1120 times.
✓ Branch 6 taken 3 times.
156014 switch (m_param_state) {
4286 10079 case INT_VALUE:
4287 10079 str->set_int(value.integer, is_unsigned_actual(), &my_charset_bin);
4288 10074 break;
4289 133333 case REAL_VALUE:
4290 133333 str->set_real(value.real, DECIMAL_NOT_SPECIFIED, &my_charset_bin);
4291 133333 break;
4292 2994 case DECIMAL_VALUE:
4293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2994 times.
2994 if (my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, str) > 1)
4294 return &my_null_string;
4295 2994 break;
4296 221 case TIME_VALUE: {
4297 char *buf, *ptr;
4298 221 str->length(0);
4299 /*
4300 TODO: in case of error we need to notify replication
4301 that binary log contains wrong statement
4302 */
4303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 221 times.
221 if (str->reserve(MAX_DATE_STRING_REP_LENGTH + 3)) break;
4304
4305 /* Create date string inplace */
4306 221 buf = str->c_ptr_quick();
4307 221 ptr = buf;
4308 221 *ptr++ = '\'';
4309 442 ptr += my_TIME_to_str(value.time, ptr,
4310
1/2
✓ Branch 0 taken 221 times.
✗ Branch 1 not taken.
221 min(decimals, uint8{DATETIME_MAX_DECIMALS}));
4311 221 *ptr++ = '\'';
4312 221 str->length((uint32)(ptr - buf));
4313 221 break;
4314 }
4315 8264 case STRING_VALUE:
4316 case LONG_DATA_VALUE: {
4317 8264 str->length(0);
4318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8283 times.
8267 if (append_query_string(thd, thd->variables.character_set_client,
4319 &str_value, str))
4320 return nullptr;
4321 8283 break;
4322 }
4323 1120 case NULL_VALUE:
4324 1120 return &my_null_string;
4325 3 default:
4326 3 assert(0);
4327 }
4328 154905 return str;
4329 }
4330
4331 /**
4332 Convert value according to the following rules:
4333 - Convert string from client character set to the character set of
4334 connection.
4335 - Invalid character set conversions cause an error.
4336 - If resolved type is a temporal value, attempt to interpret string
4337 or numeric value as temporal value and set actual type accordingly.
4338 - Invalid conversions to temporal values are currently ignored and
4339 will cause neither errors nor warnings, and actual type is left
4340 unchanged. It is expected that later processing will issue error
4341 or warning as appropriate.
4342
4343 @returns false if success, true if error
4344 */
4345
4346 155463 bool Item_param::convert_value() {
4347
7/7
✓ Branch 0 taken 9870 times.
✓ Branch 1 taken 2909 times.
✓ Branch 2 taken 133174 times.
✓ Branch 3 taken 221 times.
✓ Branch 4 taken 8143 times.
✓ Branch 5 taken 1117 times.
✓ Branch 6 taken 57 times.
155463 switch (data_type_actual()) {
4348 9870 case MYSQL_TYPE_LONGLONG:
4349 /*
4350 If a temporal value is expected and the provided integer value can
4351 be converted to one, change the actual value accordingly.
4352 */
4353
6/6
✓ Branch 0 taken 9864 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 100 times.
✓ Branch 3 taken 9763 times.
✓ Branch 4 taken 129 times.
✓ Branch 5 taken 9763 times.
19733 if (data_type() == MYSQL_TYPE_DATE ||
4354 9864 data_type() == MYSQL_TYPE_DATETIME) {
4355 129 int status = 0;
4356 MYSQL_TIME t;
4357
1/2
✓ Branch 0 taken 129 times.
✗ Branch 1 not taken.
129 if (number_to_datetime(value.integer, &t, TIME_FUZZY_DATE, &status) ==
4358
4/4
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 96 times.
225 -1LL ||
4359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 status != 0) {
4360 33 break;
4361 }
4362 96 value.time = t;
4363
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 14 times.
96 if (value.time.time_type == MYSQL_TIMESTAMP_DATE) {
4364 82 set_data_type_actual(MYSQL_TYPE_DATE);
4365
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 } else if (value.time.time_type == MYSQL_TIMESTAMP_DATETIME) {
4366 14 set_data_type_actual(MYSQL_TYPE_DATETIME);
4367 } else if (value.time.time_type == MYSQL_TIMESTAMP_DATETIME_TZ) {
4368 if (convert_time_zone_displacement(current_thd->time_zone(),
4369 &value.time))
4370 96 return true;
4371 set_data_type_actual(MYSQL_TYPE_DATETIME);
4372 } else {
4373 // We only expect DATE and DATETIME values, not TIME.
4374 assert(value.time.time_type == MYSQL_TIMESTAMP_DATE ||
4375 value.time.time_type == MYSQL_TIMESTAMP_DATETIME);
4376 }
4377 96 return false;
4378
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 9725 times.
9763 } else if (data_type() == MYSQL_TYPE_TIME) {
4379 12 int status = 0;
4380 MYSQL_TIME t;
4381
6/8
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 10 times.
✓ Branch 7 taken 3 times.
12 if (number_to_time(value.integer, &t, &status) || status != 0) {
4382 10 break;
4383 }
4384 3 value.time = t;
4385
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (value.time.time_type == MYSQL_TIMESTAMP_TIME) {
4386 2 set_data_type_actual(MYSQL_TYPE_TIME);
4387
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (value.time.time_type == MYSQL_TIMESTAMP_DATETIME) {
4388 1 set_data_type_actual(MYSQL_TYPE_DATETIME);
4389 } else {
4390 // We only expect TIME and DATETIME values, not DATE.
4391 assert(value.time.time_type == MYSQL_TIMESTAMP_TIME ||
4392 value.time.time_type == MYSQL_TIMESTAMP_DATETIME);
4393 }
4394 3 return false;
4395 }
4396 9725 break;
4397
4398 2909 case MYSQL_TYPE_NEWDECIMAL:
4399 /*
4400 If a temporal value is expected and the provided decimal value can
4401 be converted to one, change the actual value accordingly.
4402 */
4403
6/6
✓ Branch 0 taken 2888 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 53 times.
✓ Branch 3 taken 2835 times.
✓ Branch 4 taken 74 times.
✓ Branch 5 taken 2835 times.
5797 if (data_type() == MYSQL_TYPE_DATE ||
4404 2888 data_type() == MYSQL_TYPE_DATETIME) {
4405 MYSQL_TIME t;
4406
2/4
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 74 times.
74 if (decimal_to_datetime(&decimal_value, &t, TIME_FUZZY_DATE)) {
4407 break;
4408 }
4409 74 value.time = t;
4410
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 70 times.
74 if (value.time.time_type == MYSQL_TIMESTAMP_DATE) {
4411 4 set_data_type_actual(MYSQL_TYPE_DATE);
4412
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 } else if (value.time.time_type == MYSQL_TIMESTAMP_DATETIME) {
4413 70 set_data_type_actual(MYSQL_TYPE_DATETIME);
4414 } else if (value.time.time_type == MYSQL_TIMESTAMP_DATETIME_TZ) {
4415 if (convert_time_zone_displacement(current_thd->time_zone(),
4416 &value.time))
4417 74 return true;
4418 set_data_type_actual(MYSQL_TYPE_DATETIME);
4419 } else {
4420 // We only expect DATE and DATETIME values, not TIME.
4421 assert(value.time.time_type == MYSQL_TIMESTAMP_DATE ||
4422 value.time.time_type == MYSQL_TIMESTAMP_DATETIME);
4423 }
4424 74 return false;
4425
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 2771 times.
2835 } else if (data_type() == MYSQL_TYPE_TIME) {
4426 MYSQL_TIME t;
4427
3/4
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 63 times.
64 if (decimal_to_time(&decimal_value, &t)) {
4428 1 break;
4429 }
4430 63 value.time = t;
4431
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 1 times.
63 if (value.time.time_type == MYSQL_TIMESTAMP_TIME) {
4432 62 set_data_type_actual(MYSQL_TYPE_TIME);
4433
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (value.time.time_type == MYSQL_TIMESTAMP_DATETIME) {
4434 1 set_data_type_actual(MYSQL_TYPE_DATETIME);
4435 } else {
4436 // We only expect TIME and DATETIME values, not DATE.
4437 assert(value.time.time_type == MYSQL_TIMESTAMP_TIME ||
4438 value.time.time_type == MYSQL_TIMESTAMP_DATETIME);
4439 }
4440 63 return false;
4441 }
4442 2771 break;
4443
4444 133174 case MYSQL_TYPE_DOUBLE:
4445 /*
4446 If a temporal value is expected and the provided float value can
4447 be converted to one, change the actual value accordingly.
4448 */
4449
6/6
✓ Branch 0 taken 133162 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 133137 times.
✓ Branch 4 taken 37 times.
✓ Branch 5 taken 133137 times.
266336 if (data_type() == MYSQL_TYPE_DATE ||
4450 133162 data_type() == MYSQL_TYPE_DATETIME) {
4451 MYSQL_TIME t;
4452
2/4
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37 times.
37 if (double_to_datetime(value.real, &t, TIME_FUZZY_DATE)) {
4453 break;
4454 }
4455 37 value.time = t;
4456
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 33 times.
37 if (value.time.time_type == MYSQL_TIMESTAMP_DATE) {
4457 4 set_data_type_actual(MYSQL_TYPE_DATE);
4458
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 } else if (value.time.time_type == MYSQL_TIMESTAMP_DATETIME) {
4459 33 set_data_type_actual(MYSQL_TYPE_DATETIME);
4460 } else if (value.time.time_type == MYSQL_TIMESTAMP_DATETIME_TZ) {
4461 if (convert_time_zone_displacement(current_thd->time_zone(),
4462 &value.time))
4463 37 return true;
4464 set_data_type_actual(MYSQL_TYPE_DATETIME);
4465 } else {
4466 // We only expect DATE and DATETIME values, not TIME.
4467 assert(value.time.time_type == MYSQL_TIMESTAMP_DATE ||
4468 value.time.time_type == MYSQL_TIMESTAMP_DATETIME);
4469 }
4470 37 return false;
4471
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 133124 times.
133137 } else if (data_type() == MYSQL_TYPE_TIME) {
4472 MYSQL_TIME t;
4473
3/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 3 times.
13 if (double_to_time(value.real, &t)) {
4474 10 break;
4475 }
4476 3 value.time = t;
4477
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (value.time.time_type == MYSQL_TIMESTAMP_TIME) {
4478 2 set_data_type_actual(MYSQL_TYPE_TIME);
4479
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (value.time.time_type == MYSQL_TIMESTAMP_DATETIME) {
4480 1 set_data_type_actual(MYSQL_TYPE_DATETIME);
4481 } else {
4482 // We only expect TIME and DATETIME values, not DATE.
4483 assert(value.time.time_type == MYSQL_TIMESTAMP_TIME ||
4484 value.time.time_type == MYSQL_TIMESTAMP_DATETIME);
4485 }
4486 3 return false;
4487 }
4488 133124 break;
4489
4490 221 case MYSQL_TYPE_DATE:
4491 case MYSQL_TYPE_TIME:
4492 case MYSQL_TYPE_DATETIME:
4493 221 break;
4494
4495 8143 case MYSQL_TYPE_VARCHAR:
4496
2/2
✓ Branch 0 taken 4796 times.
✓ Branch 1 taken 3359 times.
8143 if (is_string_type(data_type())) {
4497 size_t dummy;
4498
3/4
✓ Branch 0 taken 4815 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 239 times.
✓ Branch 3 taken 4576 times.
4796 if (String::needs_conversion(0, m_collation_source, m_collation_actual,
4499 &dummy)) {
4500 uint errors;
4501 239 StringBuffer<STRING_BUFFER_USUAL_SIZE> convert_buffer;
4502
2/4
✓ Branch 0 taken 239 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 239 times.
239 if (convert_buffer.copy(str_value.ptr(), str_value.length(),
4503 m_collation_source, m_collation_actual,
4504 &errors))
4505 return true;
4506
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 235 times.
239 if (errors > 0) {
4507 4 my_error(ER_IMPOSSIBLE_STRING_CONVERSION, MYF(0),
4508 4 m_collation_source->m_coll_name,
4509
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 m_collation_actual->m_coll_name, "parameter");
4510 4 return true;
4511 }
4512
2/4
✓ Branch 0 taken 235 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 235 times.
235 if (str_value.copy(convert_buffer)) return true;
4513
2/2
✓ Branch 0 taken 235 times.
✓ Branch 1 taken 4 times.
239 } else {
4514 4576 str_value.set_charset(m_collation_actual);
4515 }
4516
2/2
✓ Branch 0 taken 2857 times.
✓ Branch 1 taken 502 times.
3359 } else if (is_numeric_type(data_type())) {
4517 2857 const char *ptr = str_value.ptr();
4518 2857 size_t length = str_value.length();
4519 2857 const CHARSET_INFO *cs = m_collation_source;
4520 int error;
4521 const char *endptr;
4522 2857 bool check_integer = is_integer_type(data_type());
4523
2/2
✓ Branch 0 taken 1754 times.
✓ Branch 1 taken 1103 times.
2857 if (check_integer) {
4524 // First, check if string is a signed or unsigned integer
4525 1754 endptr = ptr + length;
4526
1/2
✓ Branch 0 taken 1754 times.
✗ Branch 1 not taken.
1754 value.integer = (*(cs->cset->strtoll10))(cs, ptr, &endptr, &error);
4527
4/4
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 1590 times.
✓ Branch 2 taken 1592 times.
✓ Branch 3 taken 162 times.
1918 if (length == static_cast<size_t>(endptr - ptr) ||
4528
3/4
✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 162 times.
164 check_if_only_end_space(cs, endptr, ptr + length)) {
4529
6/6
✓ Branch 0 taken 934 times.
✓ Branch 1 taken 658 times.
✓ Branch 2 taken 872 times.
✓ Branch 3 taken 62 times.
✓ Branch 4 taken 606 times.
✓ Branch 5 taken 266 times.
1592 if (!unsigned_flag && error <= 0 && value.integer >= 0) {
4530 606 set_data_type_actual(MYSQL_TYPE_LONGLONG, false);
4531 2701 return false;
4532
4/4
✓ Branch 0 taken 658 times.
✓ Branch 1 taken 328 times.
✓ Branch 2 taken 408 times.
✓ Branch 3 taken 250 times.
986 } else if (unsigned_flag && error == 0) {
4533 408 set_data_type_actual(MYSQL_TYPE_LONGLONG, true);
4534 408 return false;
4535 }
4536 }
4537 }
4538 // Next, check if it is a decimal
4539
6/6
✓ Branch 0 taken 1103 times.
✓ Branch 1 taken 740 times.
✓ Branch 2 taken 546 times.
✓ Branch 3 taken 557 times.
✓ Branch 4 taken 1286 times.
✓ Branch 5 taken 557 times.
1843 if (check_integer || data_type() == MYSQL_TYPE_NEWDECIMAL) {
4540
3/4
✓ Branch 0 taken 1286 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1150 times.
✓ Branch 3 taken 136 times.
1286 if (str2my_decimal(E_DEC_ERROR, ptr, length, cs, &decimal_value) ==
4541 E_DEC_OK) {
4542 1150 set_data_type_actual(MYSQL_TYPE_NEWDECIMAL);
4543 1150 return false;
4544 }
4545 }
4546 // Finally, check if it is a valid floating point value
4547
1/2
✓ Branch 0 taken 693 times.
✗ Branch 1 not taken.
693 value.real = my_strntod(cs, ptr, length, &endptr, &error);
4548 2079 if (error == 0 &&
4549
5/6
✓ Branch 0 taken 693 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 546 times.
✓ Branch 3 taken 147 times.
✓ Branch 4 taken 537 times.
✓ Branch 5 taken 156 times.
1239 endptr - ptr > 0 && // my_strntod() accepts empty string as 0.0e0
4550
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 535 times.
546 (length == static_cast<size_t>(endptr - ptr) ||
4551
3/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 9 times.
11 check_if_only_end_space(cs, endptr, ptr + length))) {
4552 537 set_data_type_actual(MYSQL_TYPE_DOUBLE);
4553 537 return false;
4554 }
4555
6/6
✓ Branch 0 taken 408 times.
✓ Branch 1 taken 94 times.
✓ Branch 2 taken 251 times.
✓ Branch 3 taken 157 times.
✓ Branch 4 taken 345 times.
✓ Branch 5 taken 157 times.
910 } else if (data_type() == MYSQL_TYPE_DATE ||
4556 408 data_type() == MYSQL_TYPE_DATETIME) {
4557 345 str_value.set_charset(m_collation_source);
4558 345 MYSQL_TIME_STATUS status;
4559
1/2
✓ Branch 0 taken 345 times.
✗ Branch 1 not taken.
345 if (str_to_datetime(&str_value, &value.time, TIME_FUZZY_DATE,
4560
4/4
✓ Branch 0 taken 315 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 315 times.
✓ Branch 3 taken 30 times.
660 &status) ||
4561
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 315 times.
315 status.warnings != 0) {
4562 // Nothing
4563 } else {
4564
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 218 times.
315 if (value.time.time_type == MYSQL_TIMESTAMP_DATE) {
4565 97 set_data_type_actual(MYSQL_TYPE_DATE);
4566
2/2
✓ Branch 0 taken 214 times.
✓ Branch 1 taken 4 times.
218 } else if (value.time.time_type == MYSQL_TIMESTAMP_DATETIME) {
4567 214 set_data_type_actual(MYSQL_TYPE_DATETIME);
4568
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 } else if (value.time.time_type == MYSQL_TIMESTAMP_DATETIME_TZ) {
4569
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
4 if (convert_time_zone_displacement(current_thd->time_zone(),
4570 &value.time))
4571 315 return true;
4572 4 set_data_type_actual(MYSQL_TYPE_DATETIME);
4573 } else {
4574 // We only expect DATE and DATETIME values, not TIME.
4575 assert(value.time.time_type == MYSQL_TIMESTAMP_DATE ||
4576 value.time.time_type == MYSQL_TIMESTAMP_DATETIME ||
4577 value.time.time_type == MYSQL_TIMESTAMP_DATETIME_TZ);
4578 }
4579 315 return false;
4580 }
4581
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 39 times.
157 } else if (data_type() == MYSQL_TYPE_TIME) {
4582 118 str_value.set_charset(m_collation_source);
4583 118 MYSQL_TIME_STATUS status;
4584
5/6
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 115 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 112 times.
✓ Branch 5 taken 6 times.
233 if (str_to_time(&str_value, &value.time, 0, &status) ||
4585
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 112 times.
115 status.warnings != 0) {
4586 } else {
4587
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 42 times.
112 if (value.time.time_type == MYSQL_TIMESTAMP_TIME) {
4588 70 set_data_type_actual(MYSQL_TYPE_TIME);
4589
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 2 times.
42 } else if (value.time.time_type == MYSQL_TIMESTAMP_DATETIME) {
4590 40 set_data_type_actual(MYSQL_TYPE_DATETIME);
4591
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (value.time.time_type == MYSQL_TIMESTAMP_DATETIME_TZ) {
4592
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
2 if (convert_time_zone_displacement(current_thd->time_zone(),
4593 &value.time))
4594 112 return true;
4595 2 set_data_type_actual(MYSQL_TYPE_DATETIME);
4596 } else {
4597 // We only expect TIME and DATETIME values, not DATE.
4598 assert(value.time.time_type == MYSQL_TIMESTAMP_TIME ||
4599 value.time.time_type == MYSQL_TIMESTAMP_DATETIME ||
4600 value.time.time_type == MYSQL_TIMESTAMP_DATETIME_TZ);
4601 }
4602 112 return false;
4603 }
4604 }
4605 /*
4606 str_value_ptr is returned from val_str(). It must not be allocated
4607 to prevent it's modification by val_str() invoker.
4608 */
4609 5016 str_value_ptr.set(str_value.ptr(), str_value.length(),
4610 str_value.charset());
4611 5043 break;
4612
4613 1117 case MYSQL_TYPE_NULL:
4614 1117 break;
4615 57 default:
4616 57 assert(false);
4617 }
4618 152055 return false;
4619 }
4620
4621 Item *Item_param::clone_item() const {
4622 /* see comments in the header file */
4623 switch (m_param_state) {
4624 case NULL_VALUE:
4625 return new Item_null(item_name);
4626 case INT_VALUE:
4627 return (is_unsigned_actual()
4628 ? new Item_uint(item_name, value.integer, max_length)
4629 : new Item_int(item_name, value.integer, max_length));
4630 case REAL_VALUE:
4631 return new Item_float(item_name, value.real, decimals, max_length);
4632 case STRING_VALUE:
4633 case LONG_DATA_VALUE:
4634 return new Item_string(item_name, str_value.ptr(), str_value.length(),
4635 str_value.charset());
4636 case TIME_VALUE:
4637 break;
4638 case NO_VALUE:
4639 default:
4640 assert(0);
4641 };
4642 return nullptr;
4643 }
4644
4645 897 bool Item_param::eq(const Item *arg, bool) const { return this == arg; }
4646
4647 /* End of Item_param related */
4648
4649 2265 void Item_param::print(const THD *thd, String *str,
4650 enum_query_type query_type) const {
4651
2/2
✓ Branch 0 taken 548 times.
✓ Branch 1 taken 1717 times.
2265 if (m_param_state == NO_VALUE ||
4652
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 543 times.
548 query_type & (QT_NORMALIZED_FORMAT | QT_NO_DATA_EXPANSION)) {
4653 1722 str->append('?');
4654 } else {
4655 char buffer[STRING_BUFFER_USUAL_SIZE];
4656 543 String tmp(buffer, sizeof(buffer), &my_charset_bin);
4657
1/2
✓ Branch 0 taken 543 times.
✗ Branch 1 not taken.
543 const String *res = query_val_str(thd, &tmp);
4658
2/4
✓ Branch 0 taken 543 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 543 times.
✗ Branch 3 not taken.
543 if (res != nullptr) str->append(*res);
4659 543 }
4660 2265 }
4661
4662 /**
4663 Preserve the original parameter types and values
4664 when re-preparing a prepared statement.
4665
4666 @details Copy parameter type information and conversion
4667 function pointers from a parameter of the old statement
4668 to the corresponding parameter of the new one.
4669
4670 Move parameter values from the old parameters to the new
4671 one. We simply "exchange" the values, which allows
4672 to save on allocation and character set conversion in
4673 case a parameter is a string or a blob/clob.
4674
4675 The old parameter gets the value of this one, which
4676 ensures that all memory of this parameter is freed
4677 correctly.
4678
4679 @param[in] src parameter item of the original
4680 prepared statement
4681 */
4682
4683 6299 void Item_param::set_param_type_and_swap_value(Item_param *src) {
4684 6299 m_data_type_source = src->m_data_type_source;
4685 6299 m_data_type_actual = src->m_data_type_actual;
4686 6299 m_unsigned_actual = src->m_unsigned_actual;
4687 6299 m_collation_source = src->m_collation_source;
4688 6299 m_collation_actual = src->m_collation_actual;
4689
4690 6299 null_value = src->null_value;
4691
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6299 times.
6299 assert(m_param_state == src->m_param_state);
4692 6299 value = src->value;
4693
4694 6299 decimal_value.swap(src->decimal_value);
4695 6299 str_value.swap(src->str_value);
4696 6299 str_value_ptr.swap(src->str_value_ptr);
4697 6299 }
4698
4699 /**
4700 This operation is intended to store some item value in Item_param to be
4701 used later.
4702
4703 @param it a pointer to an item in the tree
4704
4705 @return Error status
4706 @retval true on error
4707 @retval false on success
4708 */
4709
4710 226 bool Item_param::set_value(THD *, sp_rcontext *, Item **it) {
4711 226 Item *arg = *it;
4712
4713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 226 times.
226 if (arg->is_null()) {
4714 set_null();
4715 return false;
4716 }
4717
4718 226 null_value = false;
4719
4720
4/5
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 103 times.
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
226 switch (arg->result_type()) {
4721 79 case STRING_RESULT: {
4722 char str_buffer[STRING_BUFFER_USUAL_SIZE];
4723 79 String sv_buffer(str_buffer, sizeof(str_buffer), &my_charset_bin);
4724
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 String *sv = arg->val_str(&sv_buffer);
4725
4726
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 if (!sv) return true;
4727
4728
2/4
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 79 times.
✗ Branch 3 not taken.
79 set_str(sv->c_ptr_safe(), sv->length());
4729
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 str_value_ptr.set(str_value.ptr(), str_value.length(),
4730 str_value.charset());
4731 79 collation.set(str_value.charset(), DERIVATION_COERCIBLE);
4732 79 break;
4733
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 }
4734
4735 24 case REAL_RESULT:
4736 24 set_double(arg->val_real());
4737 24 break;
4738
4739 103 case INT_RESULT:
4740 103 set_int(arg->val_int());
4741 103 break;
4742
4743 20 case DECIMAL_RESULT: {
4744 20 my_decimal dv_buf;
4745
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 my_decimal *dv = arg->val_decimal(&dv_buf);
4746
4747
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!dv) return true;
4748
4749
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 set_decimal(dv);
4750 20 break;
4751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 }
4752
4753 default:
4754 /* That can not happen. */
4755
4756 assert(false); // Abort in debug mode.
4757
4758 set_null(); // Set to NULL in release mode.
4759 return false;
4760 }
4761
4762 226 return false;
4763 }
4764
4765 /**
4766 Setter of Item_param::m_out_param_info.
4767
4768 m_out_param_info is used to store information about store routine
4769 OUT-parameters, such as stored routine name, database, stored routine
4770 variable name. It is supposed to be set in sp_head::execute() after
4771 Item_param::set_value() is called.
4772 */
4773
4774 226 void Item_param::set_out_param_info(Send_field *info) {
4775 226 m_out_param_info = info;
4776 /*
4777 Here we set data type for an already fixed Item object.
4778 It should rather be set when resolving the CALL statement.
4779 */
4780 226 set_data_type(m_out_param_info->type);
4781 226 m_result_type = Field::result_merge_type(data_type());
4782 226 }
4783
4784 /**
4785 Getter of Item_param::m_out_param_info.
4786
4787 m_out_param_info is used to store information about store routine
4788 OUT-parameters, such as stored routine name, database, stored routine
4789 variable name. It is supposed to be retrieved in
4790 Protocol::send_parameters() during creation of OUT-parameter result set.
4791 */
4792
4793 258 const Send_field *Item_param::get_out_param_info() const {
4794 258 return m_out_param_info;
4795 }
4796
4797 /**
4798 Fill meta-data information for the corresponding column in a result set.
4799 If this is an OUT-parameter of a stored procedure, preserve meta-data of
4800 stored-routine variable.
4801
4802 @param field container for meta-data to be filled
4803 */
4804
4805 134340 void Item_param::make_field(Send_field *field) {
4806 134340 Item::make_field(field);
4807
4808
2/2
✓ Branch 0 taken 134175 times.
✓ Branch 1 taken 165 times.
134340 if (!m_out_param_info) return;
4809
4810 /*
4811 This is an OUT-parameter of stored procedure. We should use
4812 OUT-parameter info to fill out the names.
4813 */
4814
4815 165 field->db_name = m_out_param_info->db_name;
4816 165 field->table_name = m_out_param_info->table_name;
4817 165 field->org_table_name = m_out_param_info->org_table_name;
4818 165 field->col_name = m_out_param_info->col_name;
4819 165 field->org_col_name = m_out_param_info->org_col_name;
4820
4821 165 field->length = m_out_param_info->length;
4822 165 field->charsetnr = m_out_param_info->charsetnr;
4823 165 field->flags = m_out_param_info->flags;
4824 165 field->decimals = m_out_param_info->decimals;
4825 165 field->type = m_out_param_info->type;
4826 }
4827
4828 /*
4829 Functions to convert item to field (for send_result_set_metadata)
4830 */
4831
4832 1626282 bool Item::fix_fields(THD *, Item **) {
4833
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1626287 times.
1626282 assert(is_contextualized());
4834
4835 // We do not check fields which are fixed during construction
4836
4/4
✓ Branch 0 taken 1625528 times.
✓ Branch 1 taken 759 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1625526 times.
1626287 assert(fixed == 0 || basic_const_item());
4837 1626285 fixed = true;
4838 1626285 return false;
4839 }
4840
4841 847 double Item_ref_null_helper::val_real() {
4842 847 auto tmp = super::val_real();
4843 847 owner->was_null |= null_value;
4844 847 return tmp;
4845 }
4846
4847 3352 longlong Item_ref_null_helper::val_int() {
4848 3352 auto tmp = super::val_int();
4849 3352 owner->was_null |= null_value;
4850 3352 return tmp;
4851 }
4852
4853 4 longlong Item_ref_null_helper::val_time_temporal() {
4854 4 auto tmp = super::val_time_temporal();
4855 4 owner->was_null |= null_value;
4856 4 return tmp;
4857 }
4858
4859 393 longlong Item_ref_null_helper::val_date_temporal() {
4860 393 auto tmp = super::val_date_temporal();
4861 393 owner->was_null |= null_value;
4862 393 return tmp;
4863 }
4864
4865 639 my_decimal *Item_ref_null_helper::val_decimal(my_decimal *decimal_value) {
4866 639 auto tmp = super::val_decimal(decimal_value);
4867 639 owner->was_null |= null_value;
4868 639 return tmp;
4869 }
4870
4871 bool Item_ref_null_helper::val_bool() {
4872 auto tmp = super::val_bool();
4873 owner->was_null |= null_value;
4874 return tmp;
4875 }
4876
4877 3355 String *Item_ref_null_helper::val_str(String *s) {
4878 3355 auto tmp = super::val_str(s);
4879 3355 owner->was_null |= null_value;
4880 3355 return tmp;
4881 }
4882
4883 bool Item_ref_null_helper::get_date(MYSQL_TIME *ltime,
4884 my_time_flags_t fuzzydate) {
4885 auto tmp = super::get_date(ltime, fuzzydate);
4886 owner->was_null |= null_value;
4887 return tmp;
4888 }
4889
4890 /**
4891 Mark item and Query_blocks as dependent if item was resolved in
4892 outer SELECT.
4893
4894 @param thd Current session.
4895 @param last select from which current item depend
4896 @param current current select
4897 @param resolved_item item which was resolved in outer SELECT
4898 @param mark_item item which should be marked; resolved_item will be
4899 marked anyway.
4900 */
4901
4902 21842 static void mark_as_dependent(THD *thd, Query_block *last, Query_block *current,
4903 Item_ident *resolved_item,
4904 Item_ident *mark_item) {
4905
2/2
✓ Branch 0 taken 21277 times.
✓ Branch 1 taken 565 times.
21842 const char *db_name = (resolved_item->db_name ? resolved_item->db_name : "");
4906 21842 const char *table_name =
4907
2/2
✓ Branch 0 taken 21605 times.
✓ Branch 1 taken 237 times.
21842 (resolved_item->table_name ? resolved_item->table_name : "");
4908 /* store pointer on Query_block from which item is dependent */
4909
1/2
✓ Branch 0 taken 21842 times.
✗ Branch 1 not taken.
21842 if (mark_item) mark_item->depended_from = last;
4910 /*
4911 resolved_item is the one we are resolving (and we just found that it is an
4912 outer ref), its context is surely the subquery (see assertion below), so
4913 we set depended_from for it.
4914 */
4915 21842 resolved_item->depended_from = last;
4916
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21842 times.
21842 assert(resolved_item->context->query_block == current);
4917
4918 21842 current->mark_as_dependent(last, false);
4919
2/2
✓ Branch 0 taken 2572 times.
✓ Branch 1 taken 19270 times.
21842 if (thd->lex->is_explain()) {
4920 /*
4921 UNION's result has select_number == INT_MAX which is printed as -1 and
4922 this is confusing. Instead, the number of the first SELECT in the UNION
4923 is printed as names in ORDER BY are resolved against select list of the
4924 first SELECT.
4925 */
4926 2572 uint sel_nr = (last->select_number < INT_MAX)
4927
2/2
✓ Branch 0 taken 2562 times.
✓ Branch 1 taken 10 times.
2582 ? last->select_number
4928 : last->master_query_expression()
4929 10 ->first_query_block()
4930 2572 ->select_number;
4931 5144 push_warning_printf(thd, Sql_condition::SL_NOTE, ER_WARN_FIELD_RESOLVED,
4932 ER_THD(thd, ER_WARN_FIELD_RESOLVED), db_name,
4933
2/2
✓ Branch 0 taken 2442 times.
✓ Branch 1 taken 130 times.
2572 (db_name[0] ? "." : ""), table_name,
4934
2/2
✓ Branch 0 taken 2543 times.
✓ Branch 1 taken 29 times.
2572 (table_name[0] ? "." : ""), resolved_item->field_name,
4935 current->select_number, sel_nr);
4936 }
4937 21842 }
4938
4939 /**
4940 Search a GROUP BY clause for a field with a certain name.
4941
4942 Search the GROUP BY list for a column named as find_item. When searching
4943 preference is given to columns that are qualified with the same table (and
4944 database) name as the one being searched for.
4945
4946 @param find_item the item being searched for
4947 @param group_list GROUP BY clause
4948
4949 @return
4950 - the found item on success
4951 - NULL if find_item is not in group_list
4952 */
4953
4954 2272 static Item **find_field_in_group_list(Item *find_item, ORDER *group_list) {
4955 const char *db_name;
4956 const char *table_name;
4957 const char *field_name;
4958 2272 ORDER *found_group = nullptr;
4959 2272 int found_match_degree = 0;
4960 Item_ident *cur_field;
4961 2272 int cur_match_degree = 0;
4962 char name_buff[NAME_LEN + 1];
4963
4964
4/6
✓ Branch 0 taken 2272 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2257 times.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 2272 times.
✗ Branch 5 not taken.
4529 if (find_item->type() == Item::FIELD_ITEM ||
4965
2/4
✓ Branch 0 taken 2257 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2257 times.
✗ Branch 3 not taken.
2257 find_item->type() == Item::REF_ITEM) {
4966 2272 db_name = ((Item_ident *)find_item)->db_name;
4967 2272 table_name = ((Item_ident *)find_item)->table_name;
4968 2272 field_name = ((Item_ident *)find_item)->field_name;
4969 } else
4970 return nullptr;
4971
4972
3/4
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 2140 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 132 times.
2272 if (db_name && lower_case_table_names) {
4973 /* Convert database to lower case for comparison */
4974 strmake(name_buff, db_name, sizeof(name_buff) - 1);
4975 my_casedn_str(files_charset_info, name_buff);
4976 db_name = name_buff;
4977 }
4978
4979
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2272 times.
2272 assert(field_name != nullptr);
4980
4981
2/2
✓ Branch 0 taken 2494 times.
✓ Branch 1 taken 2263 times.
4757 for (ORDER *cur_group = group_list; cur_group; cur_group = cur_group->next) {
4982
4/6
✓ Branch 0 taken 2494 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2494 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2300 times.
✓ Branch 5 taken 194 times.
2494 if ((*(cur_group->item))->real_item()->type() == Item::FIELD_ITEM) {
4983 2300 cur_field = (Item_ident *)*cur_group->item;
4984 2300 cur_match_degree = 0;
4985
4986
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2300 times.
2300 assert(cur_field->field_name != nullptr);
4987
4988
3/4
✓ Branch 0 taken 2300 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1692 times.
✓ Branch 3 taken 608 times.
2300 if (!my_strcasecmp(system_charset_info, cur_field->field_name,
4989 field_name))
4990 1692 ++cur_match_degree;
4991 else
4992 608 continue;
4993
4994
3/4
✓ Branch 0 taken 1692 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 258 times.
✓ Branch 3 taken 1434 times.
1692 if (cur_field->table_name && table_name) {
4995 /* If field_name is qualified by a table name. */
4996
3/4
✓ Branch 0 taken 258 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 250 times.
258 if (my_strcasecmp(table_alias_charset, cur_field->table_name,
4997 table_name))
4998 /* Same field names, different tables. */
4999 8 return nullptr;
5000
5001 250 ++cur_match_degree;
5002
4/4
✓ Branch 0 taken 246 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 120 times.
✓ Branch 3 taken 126 times.
250 if (cur_field->db_name && db_name) {
5003 /* If field_name is also qualified by a database name. */
5004
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 120 times.
120 if (strcmp(cur_field->db_name, db_name))
5005 /* Same field names, different databases. */
5006 return nullptr;
5007 120 ++cur_match_degree;
5008 }
5009 }
5010
5011
2/2
✓ Branch 0 taken 1683 times.
✓ Branch 1 taken 1 times.
1684 if (cur_match_degree > found_match_degree) {
5012 1683 found_match_degree = cur_match_degree;
5013 1683 found_group = cur_group;
5014
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
2 } else if (found_group && (cur_match_degree == found_match_degree) &&
5015
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 !(*(found_group->item))->eq(cur_field, false)) {
5016 /*
5017 If the current resolve candidate matches equally well as the current
5018 best match, they must reference the same column, otherwise the field
5019 is ambiguous.
5020 */
5021
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(),
5022
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 current_thd->where);
5023 1 return nullptr;
5024 }
5025 }
5026 }
5027
5028
2/2
✓ Branch 0 taken 1682 times.
✓ Branch 1 taken 581 times.
2263 if (found_group)
5029 1682 return found_group->item;
5030 else
5031 581 return nullptr;
5032 }
5033
5034 /**
5035 Resolve a column reference in a sub-select.
5036
5037 Resolve a column reference (usually inside a HAVING clause) against the
5038 SELECT and GROUP BY clauses of the query described by 'select'. The name
5039 resolution algorithm searches both the SELECT and GROUP BY clauses, and in
5040 case of a name conflict prefers GROUP BY column names over SELECT names. If
5041 both clauses contain different fields with the same names, a warning is
5042 issued that name of 'ref' is ambiguous. We extend ANSI SQL in that when no
5043 GROUP BY column is found, then a HAVING name is resolved as a possibly
5044 derived SELECT column.
5045
5046 @param thd current thread
5047 @param ref column reference being resolved
5048 @param select the select that ref is resolved against
5049
5050 @note
5051 The resolution procedure is:
5052 - Search for a column or derived column named col_ref_i [in table T_j]
5053 in the SELECT clause of Q.
5054 - Search for a column named col_ref_i [in table T_j]
5055 in the GROUP BY clause of Q.
5056 - If found different columns with the same name in GROUP BY and SELECT,
5057 issue a warning
5058 - return the found GROUP BY column if any,
5059 - else return the found SELECT column if any.
5060
5061
5062 @return
5063 - NULL - there was an error, and the error was already reported
5064 - not_found_item - the item was not resolved, no error was reported
5065 - resolved item - if the item was resolved
5066 */
5067
5068 4484 static Item **resolve_ref_in_select_and_group(THD *thd, Item_ident *ref,
5069 Query_block *select) {
5070
1/2
✓ Branch 0 taken 4484 times.
✗ Branch 1 not taken.
4484 DBUG_TRACE;
5071 4484 Item **select_ref = nullptr;
5072 4484 ORDER *group_list = select->group_list.first;
5073 uint counter;
5074 enum_resolution_type resolution;
5075
5076 /*
5077 If a query block is a table constructor, both the SELECT list and the GROUP
5078 BY list don't exist. So there is no reason to search any of the lists.
5079 Besides, for a table constructor, we don't initialize the base_ref_items
5080 array until we process all the ROW() values. So we should give up if
5081 base_ref_items is empty.
5082 */
5083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4484 times.
4484 if (select->base_ref_items.empty()) return not_found_item;
5084
5085 /*
5086 Search for a column or derived column named as 'ref' in the SELECT
5087 clause of the current select.
5088 */
5089 4484 if (!(select_ref =
5090
3/4
✓ Branch 0 taken 4484 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 4481 times.
4484 find_item_in_list(thd, ref, select->get_fields_list(), &counter,
5091 REPORT_EXCEPT_NOT_FOUND, &resolution)))
5092 3 return nullptr; /* Some error occurred. */
5093
2/2
✓ Branch 0 taken 1023 times.
✓ Branch 1 taken 3458 times.
4481 if (resolution == RESOLVED_AGAINST_ALIAS) ref->set_alias_of_expr();
5094
5095 /* If this is a non-aggregated field inside HAVING, search in GROUP BY. */
5096
7/8
✓ Branch 0 taken 3540 times.
✓ Branch 1 taken 941 times.
✓ Branch 2 taken 3540 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2272 times.
✓ Branch 5 taken 1268 times.
✓ Branch 6 taken 2272 times.
✓ Branch 7 taken 2209 times.
4481 if (select->having_fix_field && !ref->has_aggregation() && group_list) {
5097
1/2
✓ Branch 0 taken 2272 times.
✗ Branch 1 not taken.
2272 Item **group_by_ref = find_field_in_group_list(ref, group_list);
5098
5099 /* Check if the fields found in SELECT and GROUP BY are the same field. */
5100
6/6
✓ Branch 0 taken 1682 times.
✓ Branch 1 taken 590 times.
✓ Branch 2 taken 1281 times.
✓ Branch 3 taken 401 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 2271 times.
3553 if (group_by_ref && (select_ref != not_found_item) &&
5101
3/4
✓ Branch 0 taken 1281 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1280 times.
1281 !((*group_by_ref)->eq(*select_ref, false))) {
5102
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 push_warning_printf(thd, Sql_condition::SL_WARNING, ER_NON_UNIQ_ERROR,
5103
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 ER_THD(thd, ER_NON_UNIQ_ERROR), ref->full_name(),
5104 thd->where);
5105 }
5106
5107
2/2
✓ Branch 0 taken 1682 times.
✓ Branch 1 taken 590 times.
2272 if (group_by_ref != nullptr) return group_by_ref;
5108 }
5109
5110
2/2
✓ Branch 0 taken 911 times.
✓ Branch 1 taken 1888 times.
2799 if (select_ref == not_found_item) return not_found_item;
5111
5112
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1882 times.
1888 if ((*select_ref)->has_wf()) {
5113 /*
5114 We can't reference an alias to a window function expr from within
5115 a subquery or a HAVING clause
5116 */
5117
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 my_error(ER_WINDOW_INVALID_WINDOW_FUNC_ALIAS_USE, MYF(0), ref->field_name);
5118 6 return nullptr;
5119 }
5120
5121 /*
5122 The pointer in base_ref_items is nullptr if the column reference
5123 is a reference to itself, such as 'a' in:
5124
5125 SELECT (SELECT ... WHERE a = 1) AS a ...
5126
5127 Or if it's a reference to an expression that comes later in the
5128 select list, such as 'b' in:
5129
5130 SELECT (SELECT ... WHERE b = 1) AS a, (SELECT ...) AS b ...
5131
5132 Raise an error if such invalid references are encountered.
5133 */
5134
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 1801 times.
1882 if (select->base_ref_items[counter] == nullptr) {
5135
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 my_error(ER_ILLEGAL_REFERENCE, MYF(0), ref->item_name.ptr(),
5136 "forward reference in item list");
5137 81 return nullptr;
5138 }
5139
5140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1801 times.
1801 assert((*select_ref)->fixed);
5141
5142 1801 return &select->base_ref_items[counter];
5143 4484 }
5144
5145 /**
5146 Resolve the name of an outer select column reference.
5147
5148 The method resolves the column reference represented by 'this' as a column
5149 present in outer selects that contain current select.
5150
5151 In prepared statements, because of cache, find_field_in_tables()
5152 can resolve fields even if they don't belong to current context.
5153 In this case this method only finds appropriate context and marks
5154 current select as dependent. The found reference of field should be
5155 provided in 'from_field'.
5156
5157 @param[in] thd current thread
5158 @param[in,out] from_field found field reference or (Field*)not_found_field
5159 @param[in,out] reference view column if this item was resolved to a
5160 view column
5161
5162 @note
5163 This is the inner loop of Item_field::fix_fields:
5164 @code
5165 for each outer query Q_k beginning from the inner-most one
5166 {
5167 search for a column or derived column named col_ref_i
5168 [in table T_j] in the FROM clause of Q_k;
5169
5170 if such a column is not found
5171 Search for a column or derived column named col_ref_i
5172 [in table T_j] in the SELECT and GROUP clauses of Q_k.
5173 }
5174 @endcode
5175
5176 @retval
5177 1 column succefully resolved and fix_fields() should continue.
5178 @retval
5179 0 column fully fixed and fix_fields() should return false
5180 @retval
5181 -1 error occurred
5182 */
5183
5184 23626 int Item_field::fix_outer_field(THD *thd, Field **from_field,
5185 Item **reference) {
5186 23626 bool field_found = (*from_field != not_found_field);
5187 23626 bool upward_lookup = false;
5188
5189 /*
5190 If there are outer contexts (outer selects, but current select is
5191 not derived table or view) try to resolve this reference in the
5192 outer contexts.
5193
5194 We treat each subselect as a separate namespace, so that different
5195 subselects may contain columns with the same names. The subselects
5196 are searched starting from the innermost.
5197 */
5198 23626 Name_resolution_context *last_checked_context = context;
5199 23626 Item **ref = not_found_item;
5200 23626 Name_resolution_context *outer_context = context->outer_context;
5201 23626 Query_block *select = nullptr;
5202 23626 Query_expression *cur_query_expression = nullptr;
5203 23626 enum_parsing_context place = CTX_NONE;
5204 23626 Query_block *cur_query_block = context->query_block;
5205
2/2
✓ Branch 0 taken 22405 times.
✓ Branch 1 taken 1992 times.
24397 for (; outer_context; outer_context = outer_context->outer_context) {
5206 22405 select = outer_context->query_block;
5207
5208 22405 last_checked_context = outer_context;
5209 22405 upward_lookup = true;
5210
5211 /*
5212 We want to locate the qualifying query of our Item_field 'this'.
5213 'this' is simply contained in a subquery (Query_expression) which is
5214 immediately contained
5215 - in a scalar/row subquery (Item_subselect), or
5216 - in a table subquery itself immediately contained in a quantified
5217 predicate (Item_subselect) or a derived table (TABLE_LIST).
5218 'this' has an 'outer_context' where it should be searched first.
5219 'outer_context' is the context of a query block or sometimes
5220 of a specific part of a query block (e.g. JOIN... ON condition).
5221 We go up from 'context' to 'outer_context', from inner to outer
5222 subqueries. On that bottom-up path, we stop at the subquery unit which
5223 is simply contained in 'outer_context': it belongs to an
5224 Item_subselect/TABLE_LIST object which we note OUTER_CONTEXT_OBJECT.
5225 Then the search of 'this' in 'outer_context' is influenced by
5226 where OUTER_CONTEXT_OBJECT is in 'outer_context'. For example, if
5227 OUTER_CONTEXT_OBJECT is in WHERE, a search by alias is not done.
5228 Thus, given an 'outer_context' to search in, the first step is
5229 to determine OUTER_CONTEXT_OBJECT. Then we search for 'this' in
5230 'outer_context'. Then, if search is successful, we mark objects, from
5231 'context' up to 'outer_context', as follows:
5232 - OUTER_CONTEXT_OBJECT is marked as "using table map this->map()";
5233 - more inner subqueries are marked as "dependent on outer reference"
5234 (correlated, UNCACHEABLE_DEPENDENT bit)
5235 If search is not successful, retry with the yet-more-outer context
5236 (determine the new OUTER_CONTEXT_OBJECT, etc).
5237
5238 Note that any change here must be duplicated in Item_ref::fix_fields.
5239 */
5240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22405 times.
22405 DBUG_PRINT("outer_field",
5241 ("must reach target ctx (having SL#%d)", select->select_number));
5242 /*
5243 Walk from the innermost query block to the outermost until we find
5244 OUTER_CONTEXT_OBJECT; cur_query_block and cur_query_expression track where
5245 the walk currently is.
5246 */
5247 while (true) {
5248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23236 times.
23236 if (!cur_query_block) goto loop;
5249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23236 times.
23236 DBUG_PRINT("outer_field",
5250 ("in loop, in ctx of SL#%d", cur_query_block->select_number));
5251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23236 times.
23236 assert(cur_query_block != select);
5252 23236 cur_query_expression = cur_query_block->master_query_expression();
5253
2/2
✓ Branch 0 taken 22405 times.
✓ Branch 1 taken 831 times.
23236 if (cur_query_expression->outer_query_block() == select)
5254 22405 break; // the immediate container of cur_query_expression is
5255 // OUTER_CONTEXT_OBJECT
5256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 831 times.
831 DBUG_PRINT("outer_field",
5257 ("in loop, in ctx of SL#%d, not yet immediate child of target",
5258 cur_query_block->select_number));
5259 // cur_query_expression belongs to an object inside OUTER_CONTEXT_OBJECT,
5260 // mark it and go up:
5261 831 cur_query_expression->accumulate_used_tables(OUTER_REF_TABLE_BIT);
5262 831 cur_query_block = cur_query_expression->outer_query_block();
5263 }
5264
5265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22405 times.
22405 DBUG_PRINT("outer_field", ("out of loop, reached target ctx (having SL#%d)",
5266 cur_query_block->select_number));
5267
5268 // Place of OUTER_CONTEXT_OBJECT in 'outer_context' e.g. WHERE :
5269 22405 place = cur_query_expression->place();
5270
5271 // A non-lateral derived table cannot see tables of its owning query
5272
4/4
✓ Branch 0 taken 3706 times.
✓ Branch 1 taken 18699 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 3685 times.
22405 if (place == CTX_DERIVED && select->end_lateral_table == nullptr) continue;
5273
5274 /*
5275 If field was already found by first call
5276 to find_field_in_tables(), we only need to find appropriate context.
5277 */
5278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22384 times.
22384 if (field_found &&
5279 outer_context->query_block != cached_table->query_block) {
5280 DBUG_PRINT("outer_field", ("but cached is of SL#%d, continue",
5281 cached_table->query_block->select_number));
5282 continue;
5283 }
5284
5285 /*
5286 In case of a view, find_field_in_tables() writes the pointer to
5287 the found view field into '*reference', in other words, it
5288 substitutes this Item_field with the found expression.
5289 */
5290
3/4
✓ Branch 0 taken 22384 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21375 times.
✓ Branch 3 taken 1009 times.
44768 if (field_found ||
5291 22384 (*from_field = find_field_in_tables(
5292 thd, this, outer_context->first_name_resolution_table,
5293 outer_context->last_name_resolution_table, reference,
5294
2/2
✓ Branch 0 taken 21375 times.
✓ Branch 1 taken 1009 times.
22384 IGNORE_EXCEPT_NON_UNIQUE, thd->want_privilege, true)) !=
5295 not_found_field) {
5296
2/2
✓ Branch 0 taken 21374 times.
✓ Branch 1 taken 1 times.
21375 if (*from_field) {
5297
2/2
✓ Branch 0 taken 21093 times.
✓ Branch 1 taken 281 times.
21374 if (*from_field != view_ref_found) {
5298 21093 cur_query_expression->accumulate_used_tables(
5299 21093 (*from_field)->table->pos_in_table_list->map());
5300 21093 set_field(*from_field);
5301
5302
2/2
✓ Branch 0 taken 20749 times.
✓ Branch 1 taken 344 times.
21093 if (!last_checked_context->query_block->having_fix_field &&
5303
4/4
✓ Branch 0 taken 1154 times.
✓ Branch 1 taken 19595 times.
✓ Branch 2 taken 282 times.
✓ Branch 3 taken 872 times.
20749 select->group_list.elements &&
5304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 282 times.
282 (place == CTX_SELECT_LIST || place == CTX_HAVING)) {
5305 Item_outer_ref *rf;
5306 /*
5307 If an outer field is resolved in a grouping select then it
5308 is replaced for an Item_outer_ref object. Otherwise an
5309 Item_field object is used.
5310 */
5311
3/6
✓ Branch 0 taken 872 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 872 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 872 times.
872 if (!(rf = new Item_outer_ref(context, this, select))) return -1;
5312 872 rf->in_sum_func = thd->lex->in_sum_func;
5313 872 *reference = rf;
5314 // WL#6570 remove-after-qa
5315
3/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 840 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 32 times.
872 assert(thd->stmt_arena->is_regular() ||
5316 !thd->lex->is_exec_started());
5317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 872 times.
872 if (rf->fix_fields(thd, nullptr)) return -1;
5318 }
5319 /*
5320 A reference is resolved to a nest level that's outer or the same as
5321 the nest level of the enclosing set function : adjust the value of
5322 max_aggr_level for the function if it's needed.
5323 */
5324
2/2
✓ Branch 0 taken 1031 times.
✓ Branch 1 taken 20062 times.
21093 if (thd->lex->in_sum_func &&
5325 1031 thd->lex->in_sum_func->base_query_block->nest_level >=
5326
2/2
✓ Branch 0 taken 1022 times.
✓ Branch 1 taken 9 times.
1031 select->nest_level) {
5327 1022 Item::Type ref_type = (*reference)->type();
5328 1022 thd->lex->in_sum_func->max_aggr_level =
5329 2044 max(thd->lex->in_sum_func->max_aggr_level,
5330 1022 int8(select->nest_level));
5331 1022 set_field(*from_field);
5332 1022 fixed = true;
5333
2/2
✓ Branch 0 taken 754 times.
✓ Branch 1 taken 268 times.
1776 mark_as_dependent(thd, last_checked_context->query_block,
5334 1022 context->query_block, this,
5335
1/2
✓ Branch 0 taken 754 times.
✗ Branch 1 not taken.
754 ((ref_type == REF_ITEM || ref_type == FIELD_ITEM)
5336 ? (Item_ident *)(*reference)
5337 : nullptr));
5338 1022 return 0;
5339 }
5340 } else {
5341
1/2
✓ Branch 0 taken 281 times.
✗ Branch 1 not taken.
281 Item::Type ref_type = (*reference)->type();
5342 281 Used_tables ut(select);
5343 281 (void)(*reference)
5344
1/2
✓ Branch 0 taken 281 times.
✗ Branch 1 not taken.
281 ->walk(&Item::used_tables_for_level, enum_walk::SUBQUERY_POSTFIX,
5345 pointer_cast<uchar *>(&ut));
5346
1/2
✓ Branch 0 taken 281 times.
✗ Branch 1 not taken.
281 cur_query_expression->accumulate_used_tables(ut.used_tables);
5347
5348
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 277 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 3 times.
281 if (select->group_list.elements && place == CTX_HAVING) {
5349 /*
5350 If an outer field is resolved in a grouping query block then it
5351 is replaced with an Item_outer_ref object. Otherwise an
5352 Item_field object is used.
5353 */
5354 Item_outer_ref *const rf = new Item_outer_ref(
5355
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 context, down_cast<Item_ident *>(*reference), select);
5356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (rf == nullptr) return -1;
5357 1 rf->in_sum_func = thd->lex->in_sum_func;
5358 1 *reference = rf;
5359 // WL#6570 remove-after-qa
5360
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 assert(thd->stmt_arena->is_regular() ||
5361 !thd->lex->is_exec_started());
5362
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (rf->fix_fields(thd, nullptr)) return -1;
5363 }
5364
5365
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 278 times.
281 if (thd->lex->in_sum_func &&
5366 3 thd->lex->in_sum_func->base_query_block->nest_level >=
5367
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 select->nest_level)
5368 3 thd->lex->in_sum_func->max_aggr_level =
5369 6 max(thd->lex->in_sum_func->max_aggr_level,
5370 3 int8(select->nest_level));
5371
5372
2/4
✓ Branch 0 taken 281 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 281 times.
✗ Branch 3 not taken.
281 if ((*reference)->used_tables() != 0)
5373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 281 times.
281 mark_as_dependent(thd, last_checked_context->query_block,
5374
1/2
✓ Branch 0 taken 281 times.
✗ Branch 1 not taken.
281 context->query_block, this,
5375 ref_type == REF_ITEM || ref_type == FIELD_ITEM
5376 281 ? down_cast<Item_ident *>(*reference)
5377 : NULL);
5378 /*
5379 A reference to a view field had been found and we
5380 substituted it instead of this Item (find_field_in_tables
5381 does it by assigning the new value to *reference), so now
5382 we can return from this function.
5383 */
5384 281 return 0;
5385 }
5386 }
5387 20072 break;
5388 }
5389
5390 /* Search in SELECT and GROUP lists of the outer select. */
5391
4/4
✓ Branch 0 taken 607 times.
✓ Branch 1 taken 402 times.
✓ Branch 2 taken 606 times.
✓ Branch 3 taken 403 times.
1616 if (select_alias_referencable(place) &&
5392
2/2
✓ Branch 0 taken 606 times.
✓ Branch 1 taken 1 times.
607 outer_context->resolve_in_select_list) {
5393
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 547 times.
606 if (!(ref = resolve_ref_in_select_and_group(thd, this, select)))
5394 59 return -1; /* Some error occurred (e.g. ambiguous names). */
5395
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 347 times.
547 if (ref != not_found_item) {
5396 // The item which we found is already fixed
5397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 assert((*ref)->fixed);
5398 200 cur_query_expression->accumulate_used_tables((*ref)->used_tables());
5399 200 break;
5400 }
5401 }
5402
5403 /*
5404 Reference is not found in this select => this subquery depend on
5405 outer select (or we just trying to find wrong identifier, in this
5406 case it does not matter which used tables bits we set)
5407 */
5408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 750 times.
750 DBUG_PRINT("outer_field",
5409 ("out of loop, reached end of big block, continue"));
5410 750 cur_query_expression->accumulate_used_tables(OUTER_REF_TABLE_BIT);
5411 750 loop:;
5412 }
5413
5414
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22264 times.
22264 assert(ref != nullptr);
5415
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 22263 times.
22264 if (!*from_field) return -1;
5416
4/4
✓ Branch 0 taken 22063 times.
✓ Branch 1 taken 200 times.
✓ Branch 2 taken 1992 times.
✓ Branch 3 taken 20071 times.
22263 if (ref == not_found_item && *from_field == not_found_field) {
5417
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 1890 times.
1992 if (upward_lookup) {
5418 // We can't say exactly what absent table or field
5419 102 my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), thd->where);
5420 } else {
5421 /* Call find_field_in_tables only to report the error */
5422 1890 find_field_in_tables(thd, this, context->first_name_resolution_table,
5423 1890 context->last_name_resolution_table, reference,
5424 REPORT_ALL_ERRORS,
5425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1890 times.
1890 any_privileges ? 0 : thd->want_privilege, true);
5426 }
5427 1992 return -1;
5428
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 20071 times.
20271 } else if (ref != not_found_item) {
5429 Item *save;
5430 Item_ref *rf;
5431
5432 /* Should have been checked in resolve_ref_in_select_and_group(). */
5433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 assert((*ref)->fixed);
5434 /*
5435 Here, a subset of actions performed by Item_ref::set_properties
5436 is not enough. So we pass ptr to NULL into Item_ref
5437 constructor, so no initialization is performed, and call
5438 fix_fields() below.
5439 */
5440 200 save = *ref;
5441 200 *ref = nullptr; // Don't call set_properties()
5442
4/4
✓ Branch 0 taken 177 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 151 times.
✓ Branch 3 taken 26 times.
200 bool use_plain_ref = place == CTX_HAVING || !select->group_list.elements;
5443 200 rf = use_plain_ref
5444
2/2
✓ Branch 0 taken 174 times.
✓ Branch 1 taken 26 times.
400 ? new Item_ref(context, ref, db_name, table_name, field_name,
5445
2/4
✓ Branch 0 taken 174 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 174 times.
✗ Branch 3 not taken.
174 m_alias_of_expr)
5446
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
52 : new Item_outer_ref(context, ref, db_name, table_name, field_name,
5447
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
52 m_alias_of_expr, select);
5448 200 *ref = save;
5449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (!rf) return -1;
5450
5451
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 174 times.
200 if (!use_plain_ref)
5452 26 ((Item_outer_ref *)rf)->in_sum_func = thd->lex->in_sum_func;
5453
5454 200 *reference = rf;
5455 // WL#6570 remove-after-qa
5456
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 197 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
200 assert(thd->stmt_arena->is_regular() || !thd->lex->is_exec_started());
5457 /*
5458 rf is Item_ref => never substitute other items (in this case)
5459 during fix_fields() => we can use rf after fix_fields()
5460 */
5461
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 assert(!rf->fixed); // Assured by Item_ref()
5462
5/6
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 163 times.
✓ Branch 4 taken 37 times.
✓ Branch 5 taken 163 times.
200 if (rf->fix_fields(thd, reference) || rf->check_cols(1)) return -1;
5463
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 82 times.
163 if (rf->used_tables() != 0)
5464 81 mark_as_dependent(thd, last_checked_context->query_block,
5465 81 context->query_block, this, rf);
5466 163 return 0;
5467 } else {
5468 20071 mark_as_dependent(thd, last_checked_context->query_block,
5469 20071 context->query_block, this, (Item_ident *)*reference);
5470
2/2
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 19905 times.
20071 if (last_checked_context->query_block->having_fix_field) {
5471 Item_ref *rf;
5472 166 rf = new Item_ref(context,
5473 314 (cached_table->db[0] ? cached_table->db : nullptr),
5474
4/6
✓ Branch 0 taken 166 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 166 times.
✗ Branch 5 not taken.
166 cached_table->alias, field_name);
5475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 166 times.
166 if (!rf) return -1;
5476 166 *reference = rf;
5477 // WL#6570 remove-after-qa
5478
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 157 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
166 assert(thd->stmt_arena->is_regular() || !thd->lex->is_exec_started());
5479 /*
5480 rf is Item_ref => never substitute other items (in this case)
5481 during fix_fields() => we can use rf after fix_fields()
5482 */
5483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 166 times.
166 assert(!rf->fixed); // Assured by Item_ref()
5484
5/6
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 165 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 165 times.
166 if (rf->fix_fields(thd, reference) || rf->check_cols(1)) return -1;
5485 165 return 0;
5486 }
5487 }
5488 19905 return 1;
5489 }
5490
5491 /**
5492 Check if the column reference that is currently being resolved, will be set
5493 to NULL if its qualifying query returns zero rows.
5494
5495 This is true for non-aggregated column references in the SELECT list,
5496 if the query block uses aggregation without grouping. For example:
5497
5498 SELECT COUNT(*), col FROM t WHERE some_condition
5499
5500 Here, if the table `t` is empty, or `some_condition` doesn't match any rows
5501 in `t`, the query returns one row where `col` is NULL, even if `col` is a
5502 not-nullable column.
5503
5504 Such column references are rejected if the ONLY_FULL_GROUP_BY SQL mode is
5505 enabled, in a later resolution phase.
5506 */
5507 64283038 bool is_null_on_empty_table(THD *thd, Item_field *i) {
5508 /*
5509 Nullability of a column item 'i' is normally determined from table's or
5510 view's definition. Additionally, an item may be nullable because its table
5511 is on the right side of a left join; but this has been handled by
5512 propagate_nullability() before coming here (@see TABLE::set_nullable() and
5513 Field::maybe_null()).
5514 If the table is in the left part of a left join, or is in an inner join, a
5515 non-nullable item may be set to NULL (table->set_null_row()) if, during
5516 optimization, its table is found to be empty (e.g. in read_system()) or the
5517 FROM clause of the qualifying query QQ of its table is found to return no
5518 rows. This makes a case where a non-nullable 'i' is set to NULL. Certain
5519 expressions containing the item, if evaluated, may find this abnormal
5520 behaviour. Fortunately, in the scenario described above, QQ's result is
5521 generally empty and so no expression is evaluated. Then we don't even
5522 optimize subquery expressions as their optimization may lead to evaluation
5523 of the item (e.g. in create_ref_for_key()).
5524 However there is one exception where QQ's result is not empty even though
5525 FROM clause's result is: when QQ is implicitly aggregated. In that case,
5526 return_zero_rows() sets all tables' columns to NULL and any expression in
5527 QQ's SELECT list is evaluated; to prepare for this, we mark the item 'i'
5528 as nullable below.
5529 - If item is not outer reference, we can reliably know if QQ is
5530 aggregated by testing QQ->with_sum_func
5531 - if it's outer reference, QQ->with_sum_func may not yet be set, e.g. if
5532 there is single set function referenced later in subquery and not yet
5533 resolved; but then context.query_block->with_sum_func is surely set (it's
5534 set at parsing time), so we test both members.
5535 - in_sum_func is the innermost set function SF containing the item;
5536 - if item is not an outer reference, and in_sum_func is set, SF is
5537 necessarily aggregated in QQ, and will not be evaluated (just be replaced
5538 with its "clear" value 0 or NULL), so we needn't mark 'i' as nullable;
5539 - if item is an outer reference and in_sum_func is set, we cannot yet know
5540 where SF is aggregated, it depends on other arguments of SF, so make a
5541 pessimistic assumption.
5542 Finally we test resolve_place; indeed, when QQ's result is empty, we only
5543 evaluate:
5544 - SELECT list
5545 - or HAVING, but columns of HAVING are always also present in SELECT list
5546 so are Item_ref to SELECT list and get nullability from that,
5547 - or ORDER BY but actually no as it's optimized away in such single-row
5548 query. This is not true for hypergraph optimizer. So we mark item as
5549 nullable if the query is ordered. For Ex: If there are window functions in
5550 ORDER BY, the order by list is cleared but not removed (See
5551 setup_order_final()). This makes hypergraph optimizer think it needs to
5552 execute the window function. Old optimizer does short circuiting in this
5553 case treating it as a constant plan.
5554 Note: we test with_sum_func (== references a set function);
5555 agg_func_used() (== is aggregation query) would be better but is not
5556 reliable yet at this stage.
5557 */
5558 64283038 Query_block *sl = i->context->query_block;
5559 64283038 Query_block *qsl = i->depended_from;
5560
5561
2/2
✓ Branch 0 taken 19905 times.
✓ Branch 1 taken 64263133 times.
64283038 if (qsl != nullptr)
5562 30828 return qsl->resolve_place == Query_block::RESOLVE_SELECT_LIST &&
5563
6/6
✓ Branch 0 taken 10923 times.
✓ Branch 1 taken 8982 times.
✓ Branch 2 taken 1804 times.
✓ Branch 3 taken 9119 times.
✓ Branch 4 taken 49 times.
✓ Branch 5 taken 1755 times.
29073 (sl->with_sum_func || qsl->with_sum_func) &&
5564
2/2
✓ Branch 0 taken 8802 times.
✓ Branch 1 taken 366 times.
29073 qsl->group_list.elements == 0;
5565 else
5566 98237528 return (sl->resolve_place == Query_block::RESOLVE_SELECT_LIST ||
5567
3/4
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 33974281 times.
✓ Branch 2 taken 146 times.
✗ Branch 3 not taken.
33974395 (thd->lex->using_hypergraph_optimizer && sl->is_ordered())) &&
5568
6/6
✓ Branch 0 taken 33974395 times.
✓ Branch 1 taken 30288738 times.
✓ Branch 2 taken 845062 times.
✓ Branch 3 taken 29443822 times.
✓ Branch 4 taken 89288 times.
✓ Branch 5 taken 755774 times.
98326816 sl->with_sum_func && sl->group_list.elements == 0 &&
5569
2/2
✓ Branch 0 taken 527 times.
✓ Branch 1 taken 88761 times.
64352421 thd->lex->in_sum_func == nullptr;
5570 }
5571
5572 /**
5573 Resolve the name of a column reference.
5574
5575 The method resolves the column reference represented by 'this' as a column
5576 present in one of: FROM clause, SELECT clause, GROUP BY clause of a query
5577 Q, or in outer queries that contain Q.
5578
5579 The name resolution algorithm used is (where [T_j] is an optional table
5580 name that qualifies the column name):
5581
5582 @code
5583 resolve_column_reference([T_j].col_ref_i)
5584 {
5585 search for a column or derived column named col_ref_i
5586 [in table T_j] in the FROM clause of Q;
5587
5588 if such a column is NOT found AND // Lookup in outer queries.
5589 there are outer queries
5590 {
5591 for each outer query Q_k beginning from the inner-most one
5592 {
5593 search for a column or derived column named col_ref_i
5594 [in table T_j] in the FROM clause of Q_k;
5595
5596 if such a column is not found
5597 Search for a column or derived column named col_ref_i
5598 [in table T_j] in the SELECT and GROUP clauses of Q_k.
5599 }
5600 }
5601 }
5602 @endcode
5603
5604 Notice that compared to Item_ref::fix_fields, here we first search the FROM
5605 clause, and then we search the SELECT and GROUP BY clauses.
5606
5607 For the case where a table reference is already set for the field,
5608 we just need to make a call to set_field(). This is true for a cloned
5609 field used during condition pushdown to derived tables. A cloned field
5610 inherits table reference, depended_from, cached_table, context and field
5611 from the original field. set_field() ensures all other members are set
5612 correctly.
5613
5614 @param[in] thd current thread
5615 @param[in,out] reference view column if this item was resolved to a
5616 view column
5617
5618 @retval
5619 true if error
5620 @retval
5621 false on success
5622 */
5623
5624 55999572 bool Item_field::fix_fields(THD *thd, Item **reference) {
5625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55999572 times.
55999572 assert(fixed == 0);
5626 55999572 Field *from_field = not_found_field;
5627 55999572 bool outer_fixed = false;
5628
5629 Internal_error_handler_holder<View_error_handler, TABLE_LIST> view_handler(
5630
1/2
✓ Branch 0 taken 55999572 times.
✗ Branch 1 not taken.
55999572 thd, context->view_error_handler, context->view_error_handler_arg);
5631
5632
2/2
✓ Branch 0 taken 135360 times.
✓ Branch 1 taken 55864212 times.
55999572 if (table_ref) {
5633 // This is a cloned field (used during condition pushdown to derived
5634 // tables). It has table reference and the field too. Make a call to
5635 // set_field() to ensure everything else gets set correctly.
5636 135360 TABLE_LIST *orig_table_ref = table_ref;
5637
1/2
✓ Branch 0 taken 135360 times.
✗ Branch 1 not taken.
135360 set_field(field);
5638 // Note that the call to set_field() above would have set the "table_ref"
5639 // derived from field's table which in most cases is same as the already
5640 // set "table_ref". However, in case of update statements, while setting
5641 // up update_tables, table references are changed. Since condition pushdown
5642 // happens after this setup, we must make sure we set the original table
5643 // reference for the field.
5644 135360 table_ref = orig_table_ref;
5645 135360 return false;
5646 }
5647
1/2
✓ Branch 0 taken 55864226 times.
✗ Branch 1 not taken.
55864212 if (!field) // If field is not checked
5648 {
5649 /*
5650 In case of view, find_field_in_tables() write pointer to view field
5651 expression to 'reference', i.e. it substitute that expression instead
5652 of this Item_field
5653 */
5654 111728452 from_field = find_field_in_tables(
5655 55864226 thd, this, context->first_name_resolution_table,
5656
1/2
✓ Branch 0 taken 55864263 times.
✗ Branch 1 not taken.
55864226 context->last_name_resolution_table, reference,
5657
2/2
✓ Branch 0 taken 106042 times.
✓ Branch 1 taken 55758184 times.
55864226 thd->lex->use_only_table_context ? REPORT_ALL_ERRORS
5658 : IGNORE_EXCEPT_NON_UNIQUE,
5659
2/2
✓ Branch 0 taken 289797 times.
✓ Branch 1 taken 55574429 times.
55864226 any_privileges ? 0 : thd->want_privilege, true);
5660
3/4
✓ Branch 0 taken 55864275 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 177 times.
✓ Branch 3 taken 55864098 times.
55864263 if (thd->is_error()) goto error;
5661
2/2
✓ Branch 0 taken 23747 times.
✓ Branch 1 taken 55840351 times.
55864098 if (from_field == not_found_field) {
5662 int ret;
5663 /* Look up in current select's item_list to find aliased fields */
5664
2/2
✓ Branch 0 taken 414 times.
✓ Branch 1 taken 23333 times.
23747 if (thd->lex->current_query_block()->is_item_list_lookup) {
5665 uint counter;
5666 enum_resolution_type resolution;
5667
1/2
✓ Branch 0 taken 414 times.
✗ Branch 1 not taken.
414 Item **res = find_item_in_list(
5668 414 thd, this, &thd->lex->current_query_block()->fields, &counter,
5669 REPORT_EXCEPT_NOT_FOUND, &resolution);
5670
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 411 times.
532 if (!res) return true;
5671
2/2
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 294 times.
411 if (resolution == RESOLVED_AGAINST_ALIAS) set_alias_of_expr();
5672
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 293 times.
411 if (res != not_found_item) {
5673
3/4
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 93 times.
118 if ((*res)->type() == Item::FIELD_ITEM) {
5674 /*
5675 It's an Item_field referencing another Item_field in the select
5676 list.
5677 Use the field from the Item_field in the select list and leave
5678 the Item_field instance in place.
5679 */
5680
5681 25 Item_field *const item_field = (Item_field *)(*res);
5682 25 Field *const new_field = item_field->field;
5683
5684
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (new_field == nullptr) {
5685 /* The column to which we link isn't valid. */
5686 my_error(ER_BAD_FIELD_ERROR, MYF(0), item_field->item_name.ptr(),
5687 thd->where);
5688 return true;
5689 }
5690
5691
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 set_field(new_field);
5692
5693 25 cached_table = table_ref;
5694
5695 // The found column may be an outer reference
5696
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (item_field->depended_from)
5697 mark_as_dependent(thd, item_field->depended_from,
5698 context->query_block, this, this);
5699
5700 25 return false;
5701 } else {
5702 /*
5703 It's not an Item_field in the select list so we must make a new
5704 Item_ref to point to the Item in the select list and replace the
5705 Item_field created by the parser with the new Item_ref.
5706 Ex: SELECT func1(col) as c ... ORDER BY func2(c);
5707 NOTE: If we are fixing an alias reference inside ORDER/GROUP BY
5708 item tree, then we use new Item_ref as an
5709 intermediate value to resolve referenced item only.
5710 In this case the new Item_ref item is unused.
5711 */
5712
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 1 times.
93 if (resolution == RESOLVED_AGAINST_ALIAS)
5713 92 res = &thd->lex->current_query_block()->base_ref_items[counter];
5714
5715 Item_ref *rf =
5716 new Item_ref(context, res, db_name, table_name, field_name,
5717
2/4
✓ Branch 0 taken 93 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 93 times.
✗ Branch 3 not taken.
93 resolution == RESOLVED_AGAINST_ALIAS);
5718
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (rf == nullptr) return true;
5719
5720
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93 times.
93 if (!rf->fixed) {
5721 // No need for recursive resolving of aliases.
5722 const bool group_fix_field =
5723 thd->lex->current_query_block()->group_fix_field;
5724 thd->lex->current_query_block()->group_fix_field = false;
5725 bool fix_error =
5726 rf->fix_fields(thd, (Item **)&rf) || rf->check_cols(1);
5727 thd->lex->current_query_block()->group_fix_field =
5728 group_fix_field;
5729 if (fix_error) return true;
5730 }
5731 93 *reference = rf;
5732 // WL#6570 remove-after-qa
5733
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 90 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
93 assert(thd->stmt_arena->is_regular() ||
5734 !thd->lex->is_exec_started());
5735
5736 93 return false;
5737 }
5738 }
5739 }
5740
3/4
✓ Branch 0 taken 23626 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2090 times.
✓ Branch 3 taken 21536 times.
23626 if ((ret = fix_outer_field(thd, &from_field, reference)) < 0) goto error;
5741 21536 outer_fixed = true;
5742
2/2
✓ Branch 0 taken 1631 times.
✓ Branch 1 taken 19905 times.
21536 if (!ret) return false;
5743
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 55840345 times.
55840351 } else if (!from_field)
5744 6 goto error;
5745
5746 /*
5747 We should resolve this as an outer field reference if
5748 1. we haven't done it before, and
5749 2. the query_block of the table that contains this field is
5750 different from the query_block of the current name resolution
5751 context.
5752 */
5753
2/2
✓ Branch 0 taken 55840307 times.
✓ Branch 1 taken 19943 times.
55860250 if (!outer_fixed && // 1
5754
3/4
✓ Branch 0 taken 55734182 times.
✓ Branch 1 taken 106125 times.
✓ Branch 2 taken 55734191 times.
✗ Branch 3 not taken.
55840307 cached_table && cached_table->query_block &&
5755
1/2
✓ Branch 0 taken 55734202 times.
✗ Branch 1 not taken.
55734191 context->query_block && // 2
5756
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55734202 times.
55734202 cached_table->query_block != context->query_block) {
5757 int ret;
5758 if ((ret = fix_outer_field(thd, &from_field, reference)) < 0) goto error;
5759 outer_fixed = true;
5760 if (!ret) return false;
5761 }
5762
5763 /*
5764 If inside an aggregation function, set the correct aggregation level.
5765 Even if a view reference is found, the level is still the query block
5766 associated with the context of the current item:
5767 */
5768
4/6
✓ Branch 0 taken 2910505 times.
✓ Branch 1 taken 52949745 times.
✓ Branch 2 taken 2910505 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2910505 times.
55860250 assert(from_field != view_ref_found ||
5769 context->query_block ==
5770 dynamic_cast<Item_ident *>(*reference)->context->query_block);
5771
2/2
✓ Branch 0 taken 665951 times.
✓ Branch 1 taken 55194299 times.
55860250 if (thd->lex->in_sum_func &&
5772 665951 thd->lex->in_sum_func->base_query_block->nest_level ==
5773
2/2
✓ Branch 0 taken 665757 times.
✓ Branch 1 taken 194 times.
665951 context->query_block->nest_level)
5774 665757 thd->lex->in_sum_func->max_aggr_level =
5775 1331514 max(thd->lex->in_sum_func->max_aggr_level,
5776 665757 int8(context->query_block->nest_level));
5777
5778 // If view column reference, Item in *reference is completely resolved:
5779
2/2
✓ Branch 0 taken 2910505 times.
✓ Branch 1 taken 52949745 times.
55860250 if (from_field == view_ref_found) {
5780
3/4
✓ Branch 0 taken 2910505 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 2910478 times.
2910505 if (is_null_on_empty_table(thd, this)) {
5781 27 (*reference)->set_nullable(true);
5782
4/6
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✓ Branch 5 taken 5 times.
27 if ((*reference)->real_item()->type() == Item::FIELD_ITEM) {
5783 // See below for explanation.
5784 TABLE *table =
5785
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 down_cast<Item_field *>((*reference)->real_item())->field->table;
5786 22 table->set_nullable();
5787 }
5788 }
5789 2910505 return false;
5790 }
5791
5792
3/4
✓ Branch 0 taken 52949733 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 52949731 times.
52949745 if (from_field->is_hidden_by_system()) {
5793 /*
5794 This field is either hidden by the storage engine or SQL layer. In
5795 either case, report column "not found" error.
5796 */
5797
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 my_error(ER_BAD_FIELD_ERROR, MYF(0), from_field->field_name, thd->where);
5798 2 return true;
5799 }
5800
5801 // Not view reference, not outer reference; need to set properties:
5802
1/2
✓ Branch 0 taken 52949674 times.
✗ Branch 1 not taken.
52949731 set_field(from_field);
5803 } else if (thd->mark_used_columns != MARK_COLUMNS_NONE) {
5804 TABLE *table = field->table;
5805 MY_BITMAP *current_bitmap;
5806 MY_BITMAP *other_bitmap [[maybe_unused]];
5807 if (thd->mark_used_columns == MARK_COLUMNS_READ) {
5808 current_bitmap = table->read_set;
5809 other_bitmap = table->write_set;
5810 } else {
5811 current_bitmap = table->write_set;
5812 other_bitmap = table->read_set;
5813 }
5814 if (!bitmap_test_and_set(current_bitmap, field->field_index()))
5815 assert(bitmap_is_set(other_bitmap, field->field_index()));
5816 }
5817
2/2
✓ Branch 0 taken 279887 times.
✓ Branch 1 taken 52669773 times.
52949660 if (any_privileges) {
5818 const char *db, *tab;
5819 279887 db = cached_table->get_db_name();
5820 279887 tab = cached_table->get_table_name();
5821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 279887 times.
279887 assert(field->table == table_ref->table);
5822 279887 if (!(have_privileges =
5823
3/4
✓ Branch 0 taken 279887 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 279886 times.
279887 (get_column_grant(thd, &table_ref->grant, db, tab, field_name) &
5824 VIEW_ANY_ACL))) {
5825
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 my_error(ER_COLUMNACCESS_DENIED_ERROR, MYF(0), "ANY",
5826
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 thd->security_context()->priv_user().str,
5827
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 thd->security_context()->host_or_ip().str, field_name, tab);
5828 1 goto error;
5829 }
5830 }
5831 52949659 fixed = true;
5832
3/4
✓ Branch 0 taken 52949676 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9302 times.
✓ Branch 3 taken 52940374 times.
52949659 if (is_null_on_empty_table(thd, this)) {
5833 9302 set_nullable(true);
5834
5835 // The Item is now nullable, but the underlying field still isn't,
5836 // and Copy_field uses the underlying field. Thus,
5837 // ZeroRowsAggregatedIterator sets the _table_ row to NULL instead, and
5838 // thus, it needs to be nullable. This is similar to how inner tables of
5839 // outer joins need to be nullable.
5840 9302 field->table->set_nullable();
5841 }
5842 52949686 return false;
5843
5844 2274 error:
5845 2274 return true;
5846 55999579 }
5847
5848 23489759 void Item_field::bind_fields() {
5849
2/2
✓ Branch 0 taken 326987 times.
✓ Branch 1 taken 23162772 times.
23489759 if (!fixed) return;
5850
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23162772 times.
23162772 assert(field_index != NO_FIELD_INDEX);
5851 /*
5852 Check consistency of Item_field objects:
5853 - If we have no table_ref, then field must be a valid pointer.
5854 (Applicable for expressions of generated columns).
5855 - Some temporary tables used for materialization (derived tables)
5856 have permanent metadata, hence both table_ref and field are valid.
5857 - All other tables that have a valid table_ref do not have a valid
5858 field reference at this point.
5859 */
5860
14/20
✓ Branch 0 taken 12381 times.
✓ Branch 1 taken 23150391 times.
✓ Branch 2 taken 12381 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 23150896 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 23121755 times.
✓ Branch 7 taken 29321 times.
✓ Branch 8 taken 333 times.
✓ Branch 9 taken 23121279 times.
✓ Branch 10 taken 29654 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 23121585 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 23120765 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 23120892 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 23120744 times.
✓ Branch 19 taken 148 times.
23162772 assert((table_ref == nullptr && field != nullptr) ||
5861 (table_ref != nullptr &&
5862 (table_ref->is_view_or_derived() ||
5863 table_ref->is_recursive_reference()) &&
5864 field != nullptr) ||
5865 (table_ref != nullptr &&
5866 !(table_ref->is_view_or_derived() ||
5867 table_ref->is_recursive_reference()) &&
5868 field == nullptr));
5869
3/4
✓ Branch 0 taken 23150775 times.
✓ Branch 1 taken 12004 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 23150775 times.
23162779 if (table_ref != nullptr && table_ref->table == nullptr) return;
5870
2/2
✓ Branch 0 taken 23121131 times.
✓ Branch 1 taken 41648 times.
23162779 if (field == nullptr) {
5871 23121131 field = result_field = table_ref->table->field[field_index];
5872 23121131 m_orig_field_name = field->field_name;
5873 }
5874
2/2
✓ Branch 0 taken 11865 times.
✓ Branch 1 taken 23150914 times.
23162779 if (table_name == nullptr) table_name = *field->table_name;
5875 }
5876
5877 8383 Item *Item_field::safe_charset_converter(THD *thd, const CHARSET_INFO *tocs) {
5878 8383 no_constant_propagation = true;
5879 8383 return Item::safe_charset_converter(thd, tocs);
5880 }
5881
5882 96227335 void Item_field::cleanup() {
5883
1/2
✓ Branch 0 taken 96230842 times.
✗ Branch 1 not taken.
96227335 DBUG_TRACE;
5884
2/2
✓ Branch 0 taken 5331111 times.
✓ Branch 1 taken 90899731 times.
96230842 if (!fixed) return;
5885
5886 90899731 Item_ident::cleanup();
5887 /*
5888 When TABLE is detached from TABLE_LIST, field pointers are invalid,
5889 unless field objects are created as part of statement (placeholder tables).
5890 Also invalidate the original field name, since it is usually determined
5891 from the field name in the Field object.
5892 */
5893
6/6
✓ Branch 0 taken 86029423 times.
✓ Branch 1 taken 4869453 times.
✓ Branch 2 taken 85141108 times.
✓ Branch 3 taken 888243 times.
✓ Branch 4 taken 85091765 times.
✓ Branch 5 taken 5807374 times.
176040319 if (table_ref != nullptr && !table_ref->is_view_or_derived() &&
5894
2/2
✓ Branch 0 taken 85092159 times.
✓ Branch 1 taken 49284 times.
85141108 !table_ref->is_recursive_reference()) {
5895 85091765 field = nullptr;
5896 85091765 m_orig_field_name = nullptr;
5897 }
5898
5899 // Restore result field back to the initial value
5900 90899139 result_field = field;
5901
5902 /*
5903 When table_ref is NULL, table_name must be reassigned together with
5904 table pointer.
5905 */
5906
2/2
✓ Branch 0 taken 4869870 times.
✓ Branch 1 taken 86029269 times.
90899139 if (table_ref == nullptr) table_name = nullptr;
5907
5908 // Reset field before next optimization (multiple equality analysis)
5909 90899139 item_equal = nullptr;
5910 90899139 item_equal_all_join_nests = nullptr;
5911 90899139 null_value = false;
5912
2/2
✓ Branch 0 taken 90900168 times.
✓ Branch 1 taken 5330907 times.
96230250 }
5913
5914 /**
5915 Reset all aspect of a field object, so that it can be re-resolved.
5916 This is only for use in prepared CREATE TABLE statements.
5917 @todo refactor CREATE TABLE so this is no longer needed.
5918 */
5919 2475 void Item_field::reset_field() {
5920
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2475 times.
2475 assert(table_ref == nullptr);
5921 2475 fixed = false;
5922 2475 context = nullptr;
5923 2475 db_name = m_orig_db_name;
5924 2475 table_name = m_orig_table_name;
5925 2475 m_orig_field_name = field_name;
5926 2475 field = nullptr;
5927 2475 }
5928
5929 /**
5930 Find a field among specified multiple equalities.
5931
5932 The function first searches the field among multiple equalities
5933 of the current level (in the cond_equal->current_level list).
5934 If it fails, it continues searching in upper levels accessed
5935 through a pointer cond_equal->upper_levels.
5936 The search terminates as soon as a multiple equality containing
5937 the field is found.
5938
5939 @param cond_equal reference to list of multiple equalities where
5940 the field (this object) is to be looked for
5941
5942 @return
5943 - First Item_equal containing the field, if success
5944 - nullptr, otherwise
5945 */
5946
5947 5607122 Item_equal *Item_field::find_item_equal(COND_EQUAL *cond_equal) const {
5948
2/2
✓ Branch 0 taken 1164976 times.
✓ Branch 1 taken 5434075 times.
6599051 while (cond_equal) {
5949
5/8
✓ Branch 0 taken 1164894 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1164963 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3751705 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2759776 times.
✓ Branch 7 taken 991929 times.
3751704 for (Item_equal &item : cond_equal->current_level) {
5950
3/4
✓ Branch 0 taken 2759761 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 173033 times.
✓ Branch 3 taken 2586728 times.
2759765 if (item.contains(field)) return &item;
5951 }
5952 /*
5953 The field is not found in any of the multiple equalities
5954 of the current level. Look for it in upper levels
5955 */
5956 991929 cond_equal = cond_equal->upper_levels;
5957 }
5958 5434075 return nullptr;
5959 }
5960
5961 /**
5962 Check whether a field can be substituted by an equal item.
5963
5964 The function checks whether a substitution of the field
5965 occurrence for an equal item is valid.
5966
5967 @param arg *arg != NULL <-> the field is in the context where
5968 substitution for an equal item is valid
5969
5970 @note
5971 The following statement is not always true:
5972 @n
5973 x=y => F(x)=F(x/y).
5974 @n
5975 This means substitution of an item for an equal item not always
5976 yields an equavalent condition. Here's an example:
5977 @code
5978 'a'='a '
5979 (LENGTH('a')=1) != (LENGTH('a ')=2)
5980 @endcode
5981 Such a substitution is surely valid if either the substituted
5982 field is not of a STRING type or if it is an argument of
5983 a comparison predicate.
5984
5985 @retval
5986 true substitution is valid
5987 @retval
5988 false otherwise
5989 */
5990
5991 2461703 bool Item_field::subst_argument_checker(uchar **arg) {
5992
4/4
✓ Branch 0 taken 2282199 times.
✓ Branch 1 taken 179544 times.
✓ Branch 2 taken 356066 times.
✓ Branch 3 taken 1926133 times.
2461703 return (result_type() != STRING_RESULT) || (*arg);
5993 }
5994
5995 /**
5996 Convert a numeric value to a zero-filled string
5997
5998 @param[in,out] item the item to operate on
5999 @param field The field that this value is equated to
6000
6001 This function converts a numeric value to a string. In this conversion
6002 the zero-fill flag of the field is taken into account.
6003 This is required so the resulting string value can be used instead of
6004 the field reference when propagating equalities.
6005 */
6006
6007 14 static void convert_zerofill_number_to_string(Item **item,
6008 const Field_num *field) {
6009 char buff[MAX_FIELD_WIDTH], *pos;
6010 14 String tmp(buff, sizeof(buff), field->charset()), *res;
6011
6012
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 res = (*item)->val_str(&tmp);
6013
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 4 times.
14 if ((*item)->null_value)
6014
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
20 *item = new Item_null();
6015 else {
6016
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 field->prepend_zeros(res);
6017
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 pos = sql_strmake(res->ptr(), res->length());
6018
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 *item = new Item_string(pos, res->length(), field->charset());
6019
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (*item == nullptr) return;
6020 // Ensure the string has same properties as a number
6021 4 (*item)->collation.derivation = DERIVATION_NUMERIC;
6022 }
6023
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 }
6024
6025 /**
6026 Set a pointer to the multiple equality the field reference belongs to
6027 (if any).
6028
6029 The function looks for a multiple equality containing the field item
6030 among those referenced by arg.
6031 In the case such equality exists the function does the following.
6032 If the found multiple equality contains a constant, then the field
6033 reference is substituted for this constant, otherwise it sets a pointer
6034 to the multiple equality in the field item.
6035
6036
6037 @param arg reference to list of multiple equalities where
6038 the field (this object) is to be looked for
6039
6040 @note
6041 This function is supposed to be called as a callback parameter in calls
6042 of the compile method.
6043
6044 @return
6045 - pointer to the replacing constant item, if the field item was substituted
6046 - pointer to the field item, otherwise.
6047 */
6048
6049 535625 Item *Item_field::equal_fields_propagator(uchar *arg) {
6050
2/2
✓ Branch 0 taken 21705 times.
✓ Branch 1 taken 513920 times.
535625 if (no_constant_propagation) return this;
6051
1/2
✓ Branch 0 taken 513920 times.
✗ Branch 1 not taken.
513920 item_equal = find_item_equal((COND_EQUAL *)arg);
6052 513920 Item *item = nullptr;
6053
2/2
✓ Branch 0 taken 12448 times.
✓ Branch 1 taken 501472 times.
513920 if (item_equal) item = item_equal->get_const();
6054 /*
6055 Disable const propagation for items used in different comparison contexts.
6056 This must be done because, for example, Item_hex_string->val_int() is not
6057 the same as (Item_hex_string->val_str() in BINARY column)->val_int().
6058 We cannot simply disable the replacement in a particular context (
6059 e.g. <bin_col> = <int_col> AND <bin_col> = <hex_string>) since
6060 Items don't know the context they are in and there are functions like
6061 IF (<hex_string>, 'yes', 'no').
6062
6063 Also, disable const propagation if the constant is nullable and this item is
6064 not. If we were to allow propagation in this case, we would also need to
6065 propagate the new nullability up to the parents of this item.
6066 */
6067
8/8
✓ Branch 0 taken 1875 times.
✓ Branch 1 taken 512045 times.
✓ Branch 2 taken 1837 times.
✓ Branch 3 taken 38 times.
✓ Branch 4 taken 95 times.
✓ Branch 5 taken 1742 times.
✓ Branch 6 taken 512143 times.
✓ Branch 7 taken 1778 times.
515853 if (item == nullptr || !has_compatible_context(item) ||
6068
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 36 times.
1932 (item->is_nullable() && !is_nullable()))
6069 512143 item = this;
6070
5/6
✓ Branch 0 taken 1778 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 271 times.
✓ Branch 3 taken 1507 times.
✓ Branch 4 taken 271 times.
✓ Branch 5 taken 1507 times.
2049 else if (field && field->is_flag_set(ZEROFILL_FLAG) &&
6071
7/16
✓ Branch 0 taken 271 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 270 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 270 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 270 times.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
271 IS_NUM(field->type())) {
6072 /*
6073 We don't need to zero-fill timestamp columns here because they will be
6074 first converted to a string (in date/time format) and compared as such if
6075 compared with another string.
6076 */
6077
2/4
✓ Branch 0 taken 271 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 271 times.
✗ Branch 3 not taken.
271 if (item && field->type() != FIELD_TYPE_TIMESTAMP &&
6078
5/6
✓ Branch 0 taken 271 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 257 times.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 257 times.
556 cmp_context != INT_RESULT &&
6079
3/6
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
14 item->real_item()->type() != Item::REAL_ITEM)
6080
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 convert_zerofill_number_to_string(&item, (Field_num *)field);
6081 else
6082 257 item = this;
6083 }
6084 513921 return item;
6085 }
6086
6087 /**
6088 If this field is the target is the target of replacement, replace it with
6089 the info object's item or, if the item is found inside a subquery, the target
6090 is an outer reference, so we create a new Item_field, mark it accordingly
6091 and replace with that instead.
6092
6093 @param arg An info object of type Item::Item_field_replacement.
6094 @returns the resulting item, replaced or not, or nullptr if error
6095 */
6096 186 Item *Item_field::replace_item_field(uchar *arg) {
6097 186 auto *info = pointer_cast<Item::Item_field_replacement *>(arg);
6098
6099
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 114 times.
186 if (field == info->m_target) {
6100
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 4 times.
72 if (info->m_curr_block == info->m_trans_block) return info->m_item;
6101
6102 // The field is an outer reference, so we cannot reuse transformed query
6103 // block's Item_field; make a new one for this query block
6104 4 THD *const thd = current_thd;
6105
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Item_field *outer_field = new (thd->mem_root) Item_field(thd, info->m_item);
6106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (outer_field == nullptr) return nullptr; /* purecov: inspected */
6107 4 outer_field->depended_from = info->m_trans_block;
6108 4 outer_field->context = &info->m_curr_block->context;
6109 4 return outer_field;
6110 }
6111
6112 114 return this;
6113 }
6114
6115 /**
6116 Replace an Item_field for an equal Item_field that evaluated earlier
6117 (if any).
6118
6119 The function returns a pointer to an item that is taken from
6120 the very beginning of the item_equal list which the Item_field
6121 object refers to (belongs to) unless item_equal contains a constant
6122 item. In this case the function returns this constant item,
6123 (if the substitution does not require conversion).
6124 If the Item_field object does not refer any Item_equal object
6125 'this' is returned .
6126
6127 @note
6128 This function is supposed to be called as a callback parameter in calls
6129 of the thransformer method.
6130
6131 @return
6132 - pointer to a replacement Item_field if there is a better equal item or
6133 a pointer to a constant equal item;
6134 - this - otherwise.
6135 */
6136
6137 2693428 Item *Item_field::replace_equal_field(uchar *) {
6138
2/2
✓ Branch 0 taken 10533 times.
✓ Branch 1 taken 2682895 times.
2693428 if (item_equal) {
6139 10533 Item *const_item = item_equal->get_const();
6140
2/2
✓ Branch 0 taken 373 times.
✓ Branch 1 taken 10160 times.
10533 if (const_item) {
6141
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 334 times.
373 if (!has_compatible_context(const_item)) return this;
6142 334 return const_item;
6143 }
6144 10160 Item_field *subst = item_equal->get_subst_item(this);
6145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10160 times.
10160 assert(subst);
6146
3/4
✓ Branch 0 taken 2698 times.
✓ Branch 1 taken 7462 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2698 times.
10160 assert(table_ref == subst->table_ref ||
6147 table_ref->table != subst->table_ref->table);
6148
5/6
✓ Branch 0 taken 2698 times.
✓ Branch 1 taken 7462 times.
✓ Branch 2 taken 2698 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2698 times.
✓ Branch 5 taken 7462 times.
10160 if (table_ref != subst->table_ref && !field->eq(subst->field)) {
6149 // We may have to undo the substitution that is done here when setting up
6150 // hash join; the new field may be a field from a table that is not
6151 // reachable from hash join. Store which multi-equality we found the field
6152 // substitution in, so that we can go back and find a field that the hash
6153 // join can reach.
6154 2698 subst->set_item_equal_all_join_nests(item_equal);
6155 2698 return subst;
6156 }
6157 }
6158 2690357 return this;
6159 }
6160
6161 19841308 void Item::init_make_field(Send_field *tmp_field,
6162 enum enum_field_types field_type_arg) {
6163 19841308 const char *empty_name = "";
6164 19841308 tmp_field->db_name = empty_name;
6165 19841308 tmp_field->org_table_name = empty_name;
6166 19841308 tmp_field->org_col_name = empty_name;
6167 19841308 tmp_field->table_name = empty_name;
6168 19841308 tmp_field->col_name = item_name.ptr();
6169 19841321 tmp_field->charsetnr = collation.collation->number;
6170 19841309 tmp_field->flags =
6171
2/2
✓ Branch 0 taken 7213991 times.
✓ Branch 1 taken 12627330 times.
19841321 (m_nullable ? 0 : NOT_NULL_FLAG) |
6172
2/2
✓ Branch 0 taken 5243668 times.
✓ Branch 1 taken 14597641 times.
19841321 (my_binary_compare(charset_for_protocol()) ? BINARY_FLAG : 0);
6173 19841309 tmp_field->type = field_type_arg;
6174 19841309 tmp_field->length = max_length;
6175 19841309 tmp_field->decimals = decimals;
6176
2/2
✓ Branch 0 taken 3464148 times.
✓ Branch 1 taken 16377161 times.
19841309 if (unsigned_flag) tmp_field->flags |= UNSIGNED_FLAG;
6177 19841309 tmp_field->field = false;
6178 19841309 }
6179
6180 11564418 void Item::make_field(Send_field *tmp_field) {
6181 11564418 init_make_field(tmp_field, data_type());
6182 11564430 }
6183
6184 8276927 void Item_empty_string::make_field(Send_field *tmp_field) {
6185 8276927 init_make_field(tmp_field, string_field_type(max_length));
6186 8276931 }
6187
6188 /**
6189 Verifies that the input string is well-formed according to its character set.
6190 @param str input string to verify
6191 @param send_error If true, call my_error if string is not well-formed.
6192 @param truncate If true, set to null/truncate if not well-formed.
6193
6194 @return
6195 If well-formed: input string.
6196 If not well-formed:
6197 if truncate is true and strict mode: NULL pointer and we set this
6198 Item's value to NULL.
6199 if truncate is true and not strict mode: input string truncated up to
6200 last good character.
6201 if truncate is false: input string is returned.
6202 */
6203 44978151 String *Item::check_well_formed_result(String *str, bool send_error,
6204 bool truncate) {
6205 /* Check whether we got a well-formed string */
6206 44978151 const CHARSET_INFO *cs = str->charset();
6207
6208 size_t valid_length;
6209 bool length_error;
6210
6211
3/4
✓ Branch 0 taken 44978166 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4439 times.
✓ Branch 3 taken 44973727 times.
44978217 if (validate_string(cs, str->ptr(), str->length(), &valid_length,
6212 &length_error)) {
6213 4439 const char *str_end = str->ptr() + str->length();
6214 4439 const char *print_byte = str->ptr() + valid_length;
6215
1/2
✓ Branch 0 taken 4439 times.
✗ Branch 1 not taken.
4439 THD *thd = current_thd;
6216 char hexbuf[7];
6217 4439 size_t diff = min(size_t(str_end - print_byte), size_t(3));
6218
1/2
✓ Branch 0 taken 4439 times.
✗ Branch 1 not taken.
4439 octet2hex(hexbuf, print_byte, diff);
6219
4/4
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 4372 times.
✓ Branch 2 taken 65 times.
✓ Branch 3 taken 2 times.
4439 if (send_error && length_error) {
6220
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 my_error(ER_INVALID_CHARACTER_STRING, MYF(0), cs->csname, hexbuf);
6221 65 return nullptr;
6222 }
6223
4/4
✓ Branch 0 taken 4166 times.
✓ Branch 1 taken 208 times.
✓ Branch 2 taken 4164 times.
✓ Branch 3 taken 2 times.
4374 if (truncate && length_error) {
6224
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 4096 times.
4164 if (thd->is_strict_mode()) {
6225 68 null_value = true;
6226 68 str = nullptr;
6227 } else {
6228 4096 str->length(valid_length);
6229 }
6230 }
6231
1/2
✓ Branch 0 taken 4374 times.
✗ Branch 1 not taken.
4374 push_warning_printf(
6232 thd, Sql_condition::SL_WARNING, ER_INVALID_CHARACTER_STRING,
6233
1/2
✓ Branch 0 taken 4374 times.
✗ Branch 1 not taken.
4374 ER_THD(thd, ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
6234 }
6235 44978101 return str;
6236 }
6237
6238 /*
6239 Compare two items using a given collation
6240
6241 SYNOPSIS
6242 eq_by_collation()
6243 item item to compare with
6244 binary_cmp true <-> compare as binaries
6245 cs collation to use when comparing strings
6246
6247 DESCRIPTION
6248 This method works exactly as Item::eq if the collation cs coincides with
6249 the collation of the compared objects. Otherwise, first the collations that
6250 differ from cs are replaced for cs and then the items are compared by
6251 Item::eq. After the comparison the original collations of items are
6252 restored.
6253
6254 RETURN
6255 1 compared items has been detected as equal
6256 0 otherwise
6257 */
6258
6259 2994 bool Item::eq_by_collation(Item *item, bool binary_cmp,
6260 const CHARSET_INFO *cs) {
6261 2994 const CHARSET_INFO *save_cs = nullptr;
6262 2994 const CHARSET_INFO *save_item_cs = nullptr;
6263
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 2801 times.
2994 if (collation.collation != cs) {
6264 193 save_cs = collation.collation;
6265 193 collation.collation = cs;
6266 }
6267
2/2
✓ Branch 0 taken 943 times.
✓ Branch 1 taken 2051 times.
2994 if (item->collation.collation != cs) {
6268 943 save_item_cs = item->collation.collation;
6269 943 item->collation.collation = cs;
6270 }
6271 2994 bool res = eq(item, binary_cmp);
6272
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 2801 times.
2994 if (save_cs) collation.collation = save_cs;
6273
2/2
✓ Branch 0 taken 943 times.
✓ Branch 1 taken 2051 times.
2994 if (save_item_cs) item->collation.collation = save_item_cs;
6274 2994 return res;
6275 }
6276
6277 /**
6278 Create a field to hold a string value from an item.
6279
6280 If max_length > CONVERT_IF_BIGGER_TO_BLOB create a blob @n
6281 If max_length > 0 create a varchar @n
6282 If max_length == 0 create a CHAR(0)
6283
6284 @param table Table for which the field is created
6285 */
6286
6287 1066249 Field *Item::make_string_field(TABLE *table) const {
6288 Field *field;
6289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1066249 times.
1066249 assert(collation.collation);
6290
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1066237 times.
1066249 if (data_type() == MYSQL_TYPE_JSON)
6291 13 field =
6292
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 new (*THR_MALLOC) Field_json(max_length, m_nullable, item_name.ptr());
6293
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1066230 times.
1066237 else if (data_type() == MYSQL_TYPE_GEOMETRY) {
6294
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
18 field = new (*THR_MALLOC)
6295 6 Field_geom(max_length, m_nullable, item_name.ptr(),
6296
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
24 Field::GEOM_GEOMETRY, std::optional<gis::srid_t>());
6297
2/2
✓ Branch 0 taken 241422 times.
✓ Branch 1 taken 824808 times.
1066230 } else if (max_length / collation.collation->mbmaxlen >
6298 CONVERT_IF_BIGGER_TO_BLOB)
6299
1/2
✓ Branch 0 taken 241422 times.
✗ Branch 1 not taken.
724266 field = new (*THR_MALLOC) Field_blob(
6300
1/2
✓ Branch 0 taken 241422 times.
✗ Branch 1 not taken.
724266 max_length, m_nullable, item_name.ptr(), collation.collation, true);
6301 /* Item_type_holder holds the exact type, do not change it */
6302
4/4
✓ Branch 0 taken 809493 times.
✓ Branch 1 taken 15315 times.
✓ Branch 2 taken 809176 times.
✓ Branch 3 taken 15633 times.
1634302 else if (max_length > 0 &&
6303
4/4
✓ Branch 0 taken 124455 times.
✓ Branch 1 taken 685039 times.
✓ Branch 2 taken 124137 times.
✓ Branch 3 taken 318 times.
809493 (type() != Item::TYPE_HOLDER || data_type() != MYSQL_TYPE_STRING))
6304
1/2
✓ Branch 0 taken 809176 times.
✗ Branch 1 not taken.
2427527 field = new (*THR_MALLOC) Field_varstring(
6305
1/2
✓ Branch 0 taken 809175 times.
✗ Branch 1 not taken.
2427528 max_length, m_nullable, item_name.ptr(), table->s, collation.collation);
6306 else
6307
1/2
✓ Branch 0 taken 15633 times.
✗ Branch 1 not taken.
46899 field = new (*THR_MALLOC) Field_string(
6308
1/2
✓ Branch 0 taken 15633 times.
✗ Branch 1 not taken.
46899 max_length, m_nullable, item_name.ptr(), collation.collation);
6309
1/2
✓ Branch 0 taken 1066249 times.
✗ Branch 1 not taken.
1066249 if (field) field->init(table);
6310 1066248 return field;
6311 }
6312
6313 /**
6314 Create a field based on field_type of argument.
6315
6316 For now, this is only used to create a field for
6317 IFNULL(x,something) and time functions
6318
6319 @return Created field
6320 @retval NULL error
6321 */
6322
6323 1832464 Field *Item::tmp_table_field_from_field_type(TABLE *table,
6324 bool fixed_length) const {
6325 /*
6326 The field functions defines a field to be not null if null_ptr is not 0
6327 */
6328 Field *field;
6329
6330
19/20
✓ Branch 0 taken 9318 times.
✓ Branch 1 taken 1155 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 201466 times.
✓ Branch 4 taken 949003 times.
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 59936 times.
✓ Branch 7 taken 13 times.
✓ Branch 8 taken 257 times.
✓ Branch 9 taken 29761 times.
✓ Branch 10 taken 53 times.
✓ Branch 11 taken 147731 times.
✓ Branch 12 taken 8 times.
✓ Branch 13 taken 4 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 352 times.
✓ Branch 16 taken 418034 times.
✓ Branch 17 taken 14861 times.
✓ Branch 18 taken 201 times.
✓ Branch 19 taken 290 times.
1832464 switch (data_type()) {
6331 9318 case MYSQL_TYPE_DECIMAL:
6332 case MYSQL_TYPE_NEWDECIMAL:
6333 9318 field = Field_new_decimal::create_from_item(this);
6334 9318 break;
6335 1155 case MYSQL_TYPE_TINY:
6336
1/2
✓ Branch 0 taken 1155 times.
✗ Branch 1 not taken.
3465 field = new (*THR_MALLOC)
6337
1/2
✓ Branch 0 taken 1155 times.
✗ Branch 1 not taken.
3465 Field_tiny(max_length, m_nullable, item_name.ptr(), unsigned_flag);
6338 1155 break;
6339 16 case MYSQL_TYPE_SHORT:
6340
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
48 field = new (*THR_MALLOC)
6341
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
48 Field_short(max_length, m_nullable, item_name.ptr(), unsigned_flag);
6342 16 break;
6343 201466 case MYSQL_TYPE_LONG:
6344
1/2
✓ Branch 0 taken 201466 times.
✗ Branch 1 not taken.
604398 field = new (*THR_MALLOC)
6345
1/2
✓ Branch 0 taken 201466 times.
✗ Branch 1 not taken.
604398 Field_long(max_length, m_nullable, item_name.ptr(), unsigned_flag);
6346 201466 break;
6347 949003 case MYSQL_TYPE_LONGLONG:
6348
1/2
✓ Branch 0 taken 949003 times.
✗ Branch 1 not taken.
2847009 field = new (*THR_MALLOC) Field_longlong(max_length, m_nullable,
6349
1/2
✓ Branch 0 taken 949003 times.
✗ Branch 1 not taken.
2847009 item_name.ptr(), unsigned_flag);
6350 949003 break;
6351 5 case MYSQL_TYPE_FLOAT:
6352
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
15 field = new (*THR_MALLOC) Field_float(
6353
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
15 max_length, m_nullable, item_name.ptr(), decimals, unsigned_flag);
6354 5 break;
6355 59936 case MYSQL_TYPE_DOUBLE:
6356
1/2
✓ Branch 0 taken 59936 times.
✗ Branch 1 not taken.
179808 field = new (*THR_MALLOC) Field_double(
6357
1/2
✓ Branch 0 taken 59936 times.
✗ Branch 1 not taken.
179808 max_length, m_nullable, item_name.ptr(), decimals, unsigned_flag);
6358 59936 break;
6359 13 case MYSQL_TYPE_INT24:
6360
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
39 field = new (*THR_MALLOC)
6361
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
39 Field_medium(max_length, m_nullable, item_name.ptr(), unsigned_flag);
6362 13 break;
6363 257 case MYSQL_TYPE_DATE:
6364 case MYSQL_TYPE_NEWDATE:
6365
2/4
✓ Branch 0 taken 257 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 257 times.
✗ Branch 3 not taken.
257 field = new (*THR_MALLOC) Field_newdate(m_nullable, item_name.ptr());
6366 257 break;
6367 29761 case MYSQL_TYPE_TIME:
6368 29761 field =
6369
2/4
✓ Branch 0 taken 29761 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29761 times.
✗ Branch 3 not taken.
29761 new (*THR_MALLOC) Field_timef(m_nullable, item_name.ptr(), decimals);
6370 29761 break;
6371 53 case MYSQL_TYPE_TIMESTAMP:
6372
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
159 field = new (*THR_MALLOC)
6373
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
159 Field_timestampf(m_nullable, item_name.ptr(), decimals);
6374 53 break;
6375 147731 case MYSQL_TYPE_DATETIME:
6376
1/2
✓ Branch 0 taken 147731 times.
✗ Branch 1 not taken.
443193 field = new (*THR_MALLOC)
6377
1/2
✓ Branch 0 taken 147731 times.
✗ Branch 1 not taken.
443193 Field_datetimef(m_nullable, item_name.ptr(), decimals);
6378 147731 break;
6379 8 case MYSQL_TYPE_YEAR:
6380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 assert(max_length == 4); // Field_year is only for length 4.
6381
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 field = new (*THR_MALLOC) Field_year(m_nullable, item_name.ptr());
6382 8 break;
6383 4 case MYSQL_TYPE_BIT:
6384
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
12 field = new (*THR_MALLOC)
6385
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
12 Field_bit_as_char(max_length, m_nullable, item_name.ptr());
6386 4 break;
6387 case MYSQL_TYPE_INVALID:
6388 case MYSQL_TYPE_BOOL:
6389 default:
6390 /* This case should never be chosen */
6391 assert(0);
6392 /* If something goes awfully wrong, it's better to get a string than die
6393 */
6394 352 case MYSQL_TYPE_STRING:
6395 case MYSQL_TYPE_NULL:
6396
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 352 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
352 if (fixed_length && max_length <= CONVERT_IF_BIGGER_TO_BLOB) {
6397 field = new (*THR_MALLOC) Field_string(
6398 max_length, m_nullable, item_name.ptr(), collation.collation);
6399 break;
6400 }
6401 /* Fall through to make_string_field() */
6402 [[fallthrough]];
6403 case MYSQL_TYPE_ENUM:
6404 case MYSQL_TYPE_SET:
6405 case MYSQL_TYPE_VAR_STRING:
6406 case MYSQL_TYPE_VARCHAR:
6407 418386 return make_string_field(table);
6408 14861 case MYSQL_TYPE_TINY_BLOB:
6409 case MYSQL_TYPE_MEDIUM_BLOB:
6410 case MYSQL_TYPE_LONG_BLOB:
6411 case MYSQL_TYPE_BLOB:
6412
2/2
✓ Branch 0 taken 14673 times.
✓ Branch 1 taken 188 times.
14861 if (this->type() == Item::TYPE_HOLDER)
6413
1/2
✓ Branch 0 taken 14673 times.
✗ Branch 1 not taken.
44019 field = new (*THR_MALLOC) Field_blob(
6414
1/2
✓ Branch 0 taken 14673 times.
✗ Branch 1 not taken.
44019 max_length, m_nullable, item_name.ptr(), collation.collation, true);
6415 else
6416
1/2
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
564 field = new (*THR_MALLOC)
6417 188 Field_blob(max_length, m_nullable, item_name.ptr(),
6418
1/2
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
376 collation.collation, false);
6419 14861 break; // Blob handled outside of case
6420 201 case MYSQL_TYPE_GEOMETRY:
6421
2/4
✓ Branch 0 taken 201 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 201 times.
✗ Branch 3 not taken.
603 field = new (*THR_MALLOC) Field_geom(
6422
1/2
✓ Branch 0 taken 201 times.
✗ Branch 1 not taken.
603 max_length, m_nullable, item_name.ptr(), get_geometry_type(), {});
6423 201 break;
6424 290 case MYSQL_TYPE_JSON:
6425 290 field =
6426
2/4
✓ Branch 0 taken 290 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 290 times.
✗ Branch 3 not taken.
290 new (*THR_MALLOC) Field_json(max_length, m_nullable, item_name.ptr());
6427 }
6428
1/2
✓ Branch 0 taken 1414078 times.
✗ Branch 1 not taken.
1414078 if (field) field->init(table);
6429 1414078 return field;
6430 }
6431
6432 /* ARGSUSED */
6433 6060834 void Item_field::make_field(Send_field *tmp_field) {
6434 6060834 field->make_send_field(tmp_field);
6435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6060837 times.
6060837 assert(tmp_field->table_name != nullptr);
6436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6060837 times.
6060837 assert(item_name.is_set());
6437 6060837 tmp_field->col_name = item_name.ptr(); // Use user supplied name
6438
2/2
✓ Branch 0 taken 5977772 times.
✓ Branch 1 taken 83062 times.
6060834 tmp_field->table_name = table_name != nullptr ? table_name : "";
6439
2/2
✓ Branch 0 taken 5743892 times.
✓ Branch 1 taken 316942 times.
6060834 tmp_field->db_name = m_orig_db_name != nullptr ? m_orig_db_name : "";
6440 6060834 tmp_field->org_table_name =
6441
2/2
✓ Branch 0 taken 5321214 times.
✓ Branch 1 taken 739620 times.
6060834 m_orig_table_name != nullptr ? m_orig_table_name : "";
6442 6060834 tmp_field->org_col_name =
6443
2/2
✓ Branch 0 taken 6054508 times.
✓ Branch 1 taken 6326 times.
6060834 m_orig_field_name != nullptr ? m_orig_field_name : "";
6444 6060834 tmp_field->field = true;
6445 6060834 }
6446
6447 /**
6448 Copies/converts data from “from” to “to”, but is faster on repeated execution
6449 with the same “to” field, as it caches the fields_are_memcpyable() and
6450 pack_length() calls. These are not terribly expensive in themselves, but it
6451 adds up to 5–10% in DBT-3 Q1 due to the repeated calls.
6452
6453 The “from” field _must_ correspond to the same last_to / to_is_memcpyable pair
6454 as earlier calls, unless last_to is cleared to nullptr.
6455 */
6456 278499662 static inline type_conversion_status field_conv_with_cache(
6457 Field *to, Field *from, Field **last_to, uint32_t *to_is_memcpyable) {
6458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 278499956 times.
278499662 assert(to->field_ptr() != from->field_ptr());
6459
2/2
✓ Branch 0 taken 5902903 times.
✓ Branch 1 taken 272597053 times.
278499956 if (to != *last_to) {
6460 5902903 *last_to = to;
6461
2/2
✓ Branch 0 taken 4450816 times.
✓ Branch 1 taken 1452070 times.
5902903 if (fields_are_memcpyable(to, from)) {
6462 4450816 *to_is_memcpyable = to->pack_length();
6463 } else {
6464 1452070 *to_is_memcpyable = -1;
6465 }
6466 }
6467
2/2
✓ Branch 0 taken 144631938 times.
✓ Branch 1 taken 133868015 times.
278499953 if (*to_is_memcpyable != static_cast<uint32_t>(-1)) {
6468 144631938 memcpy(to->field_ptr(), from->field_ptr(), *to_is_memcpyable);
6469 144631962 return TYPE_OK;
6470 } else {
6471 133868015 return field_conv_slow(to, from);
6472 }
6473 }
6474
6475 /**
6476 Set a field's value from a item.
6477 */
6478
6479 4798754 void Item_field::save_org_in_field(Field *to) {
6480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4798754 times.
4798754 if (field == to) {
6481 assert(null_value == field->is_null());
6482 return;
6483
2/2
✓ Branch 0 taken 2203 times.
✓ Branch 1 taken 4796551 times.
4798754 } else if (field->is_null()) {
6484 2203 null_value = true;
6485 2203 set_field_to_null_with_conversions(to, true);
6486 } else {
6487 4796551 to->set_notnull();
6488 4796551 field_conv_with_cache(to, field, &last_org_destination_field,
6489 &last_org_destination_field_memcpyable);
6490 4796551 null_value = false;
6491 }
6492 }
6493
6494 283430262 type_conversion_status Item_field::save_in_field_inner(Field *to,
6495 bool no_conversions) {
6496
1/2
✓ Branch 0 taken 283430827 times.
✗ Branch 1 not taken.
283430262 DBUG_TRACE;
6497
3/4
✓ Branch 0 taken 283430786 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9726413 times.
✓ Branch 3 taken 273704373 times.
283430827 if (field->is_null()) {
6498 9726413 null_value = true;
6499 const type_conversion_status status =
6500
1/2
✓ Branch 0 taken 9726413 times.
✗ Branch 1 not taken.
9726413 set_field_to_null_with_conversions(to, no_conversions);
6501 9726413 return status;
6502 }
6503
1/2
✓ Branch 0 taken 273704339 times.
✗ Branch 1 not taken.
273704373 to->set_notnull();
6504 273704339 null_value = false;
6505
6506 /*
6507 If we're setting the same field as the one we're reading from there's
6508 nothing to do. This can happen in 'SET x = x' type of scenarios.
6509 */
6510
2/2
✓ Branch 0 taken 874 times.
✓ Branch 1 taken 273703465 times.
273704339 if (to == field) {
6511 874 return TYPE_OK;
6512 }
6513
1/2
✓ Branch 0 taken 273703230 times.
✗ Branch 1 not taken.
273703465 return field_conv_with_cache(to, field, &last_destination_field,
6514 273703230 &last_destination_field_memcpyable);
6515 283430517 }
6516
6517 /**
6518 Store null in field.
6519
6520 This is used on INSERT.
6521 Allow NULL to be inserted in timestamp and auto_increment values.
6522
6523 @param field Field where we want to store NULL
6524 @param no_conversions Set to 1 if we should return 1 if field can't
6525 take null values.
6526 If set to 0 we will do store the 'default value'
6527 if the field is a special field. If not we will
6528 give an error.
6529
6530 @retval
6531 0 ok
6532 @retval
6533 1 Field doesn't support NULL values and can't handle 'field = NULL'
6534 */
6535
6536 4161973 type_conversion_status Item_null::save_in_field_inner(Field *field,
6537 bool no_conversions) {
6538 4161973 return set_field_to_null_with_conversions(field, no_conversions);
6539 }
6540
6541 326786413 type_conversion_status Item::save_in_field(Field *field, bool no_conversions) {
6542
1/2
✓ Branch 0 taken 326792813 times.
✗ Branch 1 not taken.
326786413 DBUG_TRACE;
6543 // In case this is a hidden column used for a functional index, insert
6544 // an error handler that catches any errors that tries to print out the
6545 // name of the hidden column. It will instead print out the functional
6546 // index name.
6547
6548 Functional_index_error_handler functional_index_error_handler(field,
6549
2/4
✓ Branch 0 taken 326791831 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 326788170 times.
✗ Branch 3 not taken.
326792813 current_thd);
6550
6551
1/2
✓ Branch 0 taken 326790052 times.
✗ Branch 1 not taken.
326788170 const type_conversion_status ret = save_in_field_inner(field, no_conversions);
6552
6553 /*
6554 If an error was raised during evaluation of the item,
6555 save_in_field_inner() might not notice and return TYPE_OK. Make
6556 sure that we return not OK if there was an error.
6557 */
6558
8/10
✓ Branch 0 taken 325556782 times.
✓ Branch 1 taken 1233270 times.
✓ Branch 2 taken 325556768 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 325556187 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2022 times.
✓ Branch 7 taken 325554165 times.
✓ Branch 8 taken 2022 times.
✓ Branch 9 taken 326787435 times.
326790052 if (ret == TYPE_OK && current_thd->is_error()) {
6559 2022 return TYPE_ERR_BAD_VALUE;
6560 }
6561 326787435 return ret;
6562 326789457 }
6563
6564 /*
6565 This implementation can lose str_value content, so if the
6566 Item uses str_value to store something, it should
6567 reimplement its ::save_in_field_inner() as Item_string, for example, does.
6568
6569 Note: all Item_XXX::val_str(str) methods must NOT rely on the fact that
6570 str != str_value. For example, see fix for bug #44743.
6571 */
6572
6573 143829970 type_conversion_status Item::save_in_field_inner(Field *field,
6574 bool no_conversions) {
6575 // Storing of arrays should be handled by specialized subclasses.
6576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143830193 times.
143829970 assert(!returns_array());
6577
6578
2/2
✓ Branch 0 taken 75913270 times.
✓ Branch 1 taken 67916650 times.
143830193 if (result_type() == STRING_RESULT) {
6579
1/2
✓ Branch 0 taken 75913263 times.
✗ Branch 1 not taken.
75913270 const enum Type typ = type();
6580
6581
4/4
✓ Branch 0 taken 103284 times.
✓ Branch 1 taken 75809979 times.
✓ Branch 2 taken 75890 times.
✓ Branch 3 taken 27394 times.
75913263 if (typ == FUNC_ITEM || typ == SUBSELECT_ITEM) {
6582 75885869 enum_field_types ft = data_type();
6583 // Avoid JSON dom/binary serialization to/from string
6584
2/2
✓ Branch 0 taken 84560 times.
✓ Branch 1 taken 75801308 times.
75885868 if (ft == MYSQL_TYPE_JSON) {
6585
3/4
✓ Branch 0 taken 84535 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22045 times.
✓ Branch 3 taken 62490 times.
84560 if (field->type() == MYSQL_TYPE_JSON) {
6586 // Store the value in the JSON binary format.
6587 22045 Field_json *f = down_cast<Field_json *>(field);
6588 22045 Json_wrapper wr;
6589
3/4
✓ Branch 0 taken 22045 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 22037 times.
22045 if (val_json(&wr)) return TYPE_ERR_BAD_VALUE;
6590
6591
3/4
✓ Branch 0 taken 1083 times.
✓ Branch 1 taken 20954 times.
✓ Branch 2 taken 1083 times.
✗ Branch 3 not taken.
22037 if (null_value) return set_field_to_null(field);
6592
6593
1/2
✓ Branch 0 taken 20954 times.
✗ Branch 1 not taken.
20954 field->set_notnull();
6594
1/2
✓ Branch 0 taken 20954 times.
✗ Branch 1 not taken.
20954 return f->store_json(&wr);
6595 22045 }
6596
6597
1/2
✓ Branch 0 taken 62485 times.
✗ Branch 1 not taken.
62490 const enum_field_types field_type = field->type();
6598
6/6
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 62215 times.
✓ Branch 2 taken 227 times.
✓ Branch 3 taken 56 times.
✓ Branch 4 taken 227 times.
✓ Branch 5 taken 62271 times.
62485 if (is_temporal_type(field_type) && field_type != MYSQL_TYPE_YEAR) {
6599 MYSQL_TIME t;
6600 227 bool res = true;
6601
2/4
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 171 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
227 switch (field_type) {
6602 56 case MYSQL_TYPE_TIME:
6603
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 res = get_time(&t);
6604 56 break;
6605 171 case MYSQL_TYPE_DATETIME:
6606 case MYSQL_TYPE_TIMESTAMP:
6607 case MYSQL_TYPE_DATE:
6608 case MYSQL_TYPE_NEWDATE:
6609
1/2
✓ Branch 0 taken 171 times.
✗ Branch 1 not taken.
171 res = get_date(&t, 0);
6610 171 break;
6611 case MYSQL_TYPE_YEAR:
6612 assert(false);
6613 default:
6614 assert(false);
6615 }
6616
2/2
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 17 times.
227 if (res) {
6617 210 null_value = true;
6618
1/2
✓ Branch 0 taken 210 times.
✗ Branch 1 not taken.
210 return set_field_to_null_with_conversions(field, no_conversions);
6619 }
6620
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 field->set_notnull();
6621
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 return field->store_time(&t);
6622 }
6623
3/4
✓ Branch 0 taken 62264 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 62192 times.
62271 if (field->type() == MYSQL_TYPE_NEWDECIMAL) {
6624 72 my_decimal decimal_value;
6625
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 my_decimal *value = val_decimal(&decimal_value);
6626
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 64 times.
72 if (null_value)
6627
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 return set_field_to_null_with_conversions(field, no_conversions);
6628
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 field->set_notnull();
6629
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 return field->store_decimal(value);
6630 72 }
6631
1/2
✓ Branch 0 taken 62187 times.
✗ Branch 1 not taken.
62192 if (field->type() == MYSQL_TYPE_INT24 ||
6632
3/4
✓ Branch 0 taken 62078 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61971 times.
✓ Branch 3 taken 107 times.
62087 field->type() == MYSQL_TYPE_TINY ||
6633
3/4
✓ Branch 0 taken 61975 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 61858 times.
✓ Branch 3 taken 117 times.
61971 field->type() == MYSQL_TYPE_SHORT ||
6634
7/8
✓ Branch 0 taken 62087 times.
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 61851 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1939 times.
✓ Branch 5 taken 59912 times.
✓ Branch 6 taken 60366 times.
✓ Branch 7 taken 1809 times.
126208 field->type() == MYSQL_TYPE_LONG ||
6635
3/4
✓ Branch 0 taken 1939 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 122 times.
✓ Branch 3 taken 1817 times.
1939 field->type() == MYSQL_TYPE_LONGLONG) {
6636
1/2
✓ Branch 0 taken 60370 times.
✗ Branch 1 not taken.
60366 longlong nr = val_int();
6637
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 60242 times.
60370 if (null_value)
6638
1/2
✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
128 return set_field_to_null_with_conversions(field, no_conversions);
6639
1/2
✓ Branch 0 taken 60254 times.
✗ Branch 1 not taken.
60242 field->set_notnull();
6640
1/2
✓ Branch 0 taken 60269 times.
✗ Branch 1 not taken.
60254 return field->store(nr, unsigned_flag);
6641 }
6642
5/6
✓ Branch 0 taken 1817 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1761 times.
✓ Branch 3 taken 56 times.
✓ Branch 4 taken 116 times.
✓ Branch 5 taken 1701 times.
3570 if (field->type() == MYSQL_TYPE_FLOAT ||
6643
3/4
✓ Branch 0 taken 1761 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 1701 times.
1761 field->type() == MYSQL_TYPE_DOUBLE) {
6644
1/2
✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
116 double nr = val_real();
6645
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 100 times.
116 if (null_value)
6646
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 return set_field_to_null_with_conversions(field, no_conversions);
6647
1/2
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
100 field->set_notnull();
6648
1/2
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
100 return field->store(nr);
6649 }
6650 }
6651 }
6652
6653 String *result;
6654 75830403 const CHARSET_INFO *cs = collation.collation;
6655 char buff[MAX_FIELD_WIDTH]; // Alloc buffer for small columns
6656 75830403 str_value.set_quick(buff, sizeof(buff), cs);
6657
1/2
✓ Branch 0 taken 75833424 times.
✗ Branch 1 not taken.
75830277 result = val_str(&str_value);
6658
4/6
✓ Branch 0 taken 75833166 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75833400 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 189 times.
✓ Branch 5 taken 75833211 times.
75833424 if (current_thd->is_error()) return TYPE_ERR_BAD_VALUE;
6659
2/2
✓ Branch 0 taken 6628717 times.
✓ Branch 1 taken 69204494 times.
75833211 if (null_value) {
6660 6628717 str_value.set_quick(nullptr, 0, cs);
6661
1/2
✓ Branch 0 taken 6628717 times.
✗ Branch 1 not taken.
6628717 return set_field_to_null_with_conversions(field, no_conversions);
6662 }
6663
6664 /* NOTE: If null_value == false, "result" must be not NULL. */
6665
6666
1/2
✓ Branch 0 taken 69204257 times.
✗ Branch 1 not taken.
69204494 field->set_notnull();
6667 type_conversion_status error =
6668
1/2
✓ Branch 0 taken 69203049 times.
✗ Branch 1 not taken.
69203833 field->store(result->ptr(), result->length(),
6669
3/4
✓ Branch 0 taken 69203833 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2459 times.
✓ Branch 3 taken 69201374 times.
69204257 field->type() == MYSQL_TYPE_JSON ? result->charset() : cs);
6670 69203049 str_value.set_quick(nullptr, 0, cs);
6671 69202858 return error;
6672 }
6673
6674
6/6
✓ Branch 0 taken 3835609 times.
✓ Branch 1 taken 64080993 times.
✓ Branch 2 taken 1426 times.
✓ Branch 3 taken 3834188 times.
✓ Branch 4 taken 1426 times.
✓ Branch 5 taken 67915181 times.
67916650 if (result_type() == REAL_RESULT && field->result_type() == STRING_RESULT) {
6675 1426 double nr = val_real();
6676
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1415 times.
1426 if (null_value)
6677 11 return set_field_to_null_with_conversions(field, no_conversions);
6678 1415 field->set_notnull();
6679 1415 return field->store(nr);
6680 }
6681
6682
2/2
✓ Branch 0 taken 3834189 times.
✓ Branch 1 taken 64081115 times.
67915181 if (result_type() == REAL_RESULT) {
6683 3834189 double nr = val_real();
6684
2/2
✓ Branch 0 taken 6179 times.
✓ Branch 1 taken 3828010 times.
3834189 if (null_value)
6685 6179 return set_field_to_null_with_conversions(field, no_conversions);
6686 3828010 field->set_notnull();
6687 3828010 return field->store(nr);
6688 }
6689
6690
2/2
✓ Branch 0 taken 5541419 times.
✓ Branch 1 taken 58539767 times.
64081115 if (result_type() == DECIMAL_RESULT) {
6691 5541419 my_decimal decimal_value;
6692
1/2
✓ Branch 0 taken 5541417 times.
✗ Branch 1 not taken.
5541415 my_decimal *value = val_decimal(&decimal_value);
6693
2/2
✓ Branch 0 taken 13989 times.
✓ Branch 1 taken 5527428 times.
5541417 if (null_value)
6694
1/2
✓ Branch 0 taken 13989 times.
✗ Branch 1 not taken.
13989 return set_field_to_null_with_conversions(field, no_conversions);
6695
1/2
✓ Branch 0 taken 5527430 times.
✗ Branch 1 not taken.
5527428 field->set_notnull();
6696
1/2
✓ Branch 0 taken 5527418 times.
✗ Branch 1 not taken.
5527430 return field->store_decimal(value);
6697 5541407 }
6698
6699 58539767 longlong nr = val_int();
6700
2/2
✓ Branch 0 taken 21061842 times.
✓ Branch 1 taken 37477755 times.
58539597 if (null_value)
6701 21061842 return set_field_to_null_with_conversions(field, no_conversions);
6702 37477755 field->set_notnull();
6703 37478094 return field->store(nr, unsigned_flag);
6704 }
6705
6706 23736021 type_conversion_status Item_string::save_in_field_inner(Field *field, bool) {
6707 String *result;
6708 23736021 result = val_str(&str_value);
6709 23736158 return save_str_value_in_field(field, result);
6710 }
6711
6712 510 type_conversion_status Item_uint::save_in_field_inner(Field *field,
6713 bool no_conversions) {
6714 /* Item_int::save_in_field_inner handles both signed and unsigned. */
6715 510 return Item_int::save_in_field_inner(field, no_conversions);
6716 }
6717
6718 /**
6719 Store an int in a field
6720
6721 @param field The field where the int value is to be stored
6722 @param nr The value to store in field
6723 @param null_value True if the value to store is NULL, false otherwise
6724 @param unsigned_flag Whether or not the int value is signed or unsigned
6725
6726 @retval TYPE_OK Storing of value went fine without warnings or errors
6727 @retval !TYPE_OK Warning/error as indicated by type_conversion_status enum
6728 value
6729 */
6730 13030281 static type_conversion_status save_int_value_in_field(Field *field, longlong nr,
6731 bool null_value,
6732 bool unsigned_flag) {
6733 // TODO: call set_field_to_null_with_conversions below
6734
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13030281 times.
13030281 if (null_value) return set_field_to_null(field);
6735 13030281 field->set_notnull();
6736 13030800 return field->store(nr, unsigned_flag);
6737 }
6738
6739 /**
6740 Store this item's int-value in a field
6741
6742 @param field The field where the int value is to be stored
6743 @param no_conversions Only applies if the value to store is NULL
6744 (null_value is true) and NULL is not allowed
6745 in field. In that case: if no_coversion is
6746 true, do nothing and return with error
6747 TYPE_ERR_NULL_CONSTRAINT_VIOLATION. If
6748 no_coversion is false, the field's default
6749 value is stored if one exists. Otherwise an
6750 error is returned.
6751
6752 @retval TYPE_OK Storing of value went fine without warnings or errors
6753 @retval !TYPE_OK Warning/error as indicated by type_conversion_status enum
6754 value
6755 */
6756 13030130 type_conversion_status Item_int::save_in_field_inner(Field *field,
6757 bool no_conversions
6758 [[maybe_unused]]) {
6759 13030130 return save_int_value_in_field(field, val_int(), null_value, unsigned_flag);
6760 }
6761
6762 79 type_conversion_status Item_temporal::save_in_field_inner(Field *field, bool) {
6763 79 const enum_field_types field_type = field->type();
6764 79 longlong nr = is_temporal_type_with_time(field_type)
6765
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 ? val_temporal_with_round(field_type, field->decimals())
6766 : val_date_temporal();
6767 // TODO: call set_field_to_null_with_conversions below
6768
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 if (null_value) return set_field_to_null(field);
6769 79 field->set_notnull();
6770 79 return field->store_packed(nr);
6771 }
6772
6773 22676 type_conversion_status Item_decimal::save_in_field_inner(Field *field, bool) {
6774
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22676 times.
22676 if (null_value) return set_field_to_null(field);
6775
6776 22676 field->set_notnull();
6777 22676 return field->store_decimal(&decimal_value);
6778 }
6779
6780 792419 bool Item_int::eq(const Item *arg, bool) const {
6781 // No need to check for null value as integer constant can't be NULL
6782
6/6
✓ Branch 0 taken 74846 times.
✓ Branch 1 taken 717582 times.
✓ Branch 2 taken 74579 times.
✓ Branch 3 taken 275 times.
✓ Branch 4 taken 74580 times.
✓ Branch 5 taken 717856 times.
792419 if (arg->basic_const_item() && arg->type() == type()) {
6783 /*
6784 We need to cast off const to call val_int(). This should be OK for
6785 a basic constant.
6786 */
6787 74580 Item *item = const_cast<Item *>(arg);
6788
3/4
✓ Branch 0 taken 58552 times.
✓ Branch 1 taken 16029 times.
✓ Branch 2 taken 58552 times.
✗ Branch 3 not taken.
74580 return item->val_int() == value && item->unsigned_flag == unsigned_flag;
6789 }
6790 717856 return false;
6791 }
6792
6793 Item *Item_int_with_ref::clone_item() const {
6794 assert(ref->const_item());
6795 /*
6796 We need to evaluate the constant to make sure it works with
6797 parameter markers.
6798 */
6799 return (ref->unsigned_flag
6800 ? new Item_uint(ref->item_name, ref->val_int(), ref->max_length)
6801 : new Item_int(ref->item_name, ref->val_int(), ref->max_length));
6802 }
6803
6804 Item *Item_time_with_ref::clone_item() const {
6805 assert(ref->const_item());
6806 /*
6807 We need to evaluate the constant to make sure it works with
6808 parameter markers.
6809 */
6810 return new Item_temporal(MYSQL_TYPE_TIME, ref->item_name,
6811 ref->val_time_temporal(), ref->max_length);
6812 }
6813
6814 Item *Item_datetime_with_ref::clone_item() const {
6815 assert(ref->const_item());
6816 /*
6817 We need to evaluate the constant to make sure it works with
6818 parameter markers.
6819 */
6820 return new Item_temporal(MYSQL_TYPE_DATETIME, ref->item_name,
6821 ref->val_date_temporal(), ref->max_length);
6822 }
6823
6824 56 void Item_temporal_with_ref::print(const THD *, String *str,
6825 enum_query_type) const {
6826 char buff[MAX_DATE_STRING_REP_LENGTH];
6827 MYSQL_TIME ltime;
6828
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 TIME_from_longlong_packed(&ltime, data_type(), value);
6829
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 str->append("'");
6830
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 my_TIME_to_str(ltime, buff, decimals);
6831
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 str->append(buff);
6832
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 str->append('\'');
6833 56 }
6834
6835 1 Item_num *Item_uint::neg() {
6836
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 Item_decimal *item = new Item_decimal(value, true);
6837 1 return item->neg();
6838 }
6839
6840 32074 static uint nr_of_decimals(const char *str, const char *end) {
6841 const char *decimal_point;
6842
6843 /* Find position for '.' */
6844 for (;;) {
6845
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 32073 times.
32074 if (str == end) return 0;
6846
4/4
✓ Branch 0 taken 31398 times.
✓ Branch 1 taken 675 times.
✓ Branch 2 taken 90 times.
✓ Branch 3 taken 31308 times.
32073 if (*str == 'e' || *str == 'E') return DECIMAL_NOT_SPECIFIED;
6847
2/2
✓ Branch 0 taken 6362 times.
✓ Branch 1 taken 24946 times.
31308 if (*str++ == '.') break;
6848 }
6849 6362 decimal_point = str;
6850
4/4
✓ Branch 0 taken 40847 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 34512 times.
✓ Branch 3 taken 6335 times.
40874 for (; str < end && my_isdigit(system_charset_info, *str); str++)
6851 ;
6852
6/6
✓ Branch 0 taken 6335 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 1542 times.
✓ Branch 3 taken 4793 times.
✓ Branch 4 taken 1541 times.
✓ Branch 5 taken 1 times.
6362 if (str < end && (*str == 'e' || *str == 'E')) return DECIMAL_NOT_SPECIFIED;
6853 /*
6854 QQ:
6855 The number of decimal digist in fact should be (str - decimal_point - 1).
6856 But it seems the result of nr_of_decimals() is never used!
6857
6858 In case of 'e' and 'E' nr_of_decimals returns DECIMAL_NOT_SPECIFIED.
6859 In case if there is no 'e' or 'E' parser code in sql_yacc.yy
6860 never calls Item_float::Item_float() - it creates Item_decimal instead.
6861
6862 The only piece of code where we call Item_float::Item_float(str, len)
6863 without having 'e' or 'E' is item_xmlfunc.cc, but this Item_float
6864 never appears in metadata itself. Changing the code to return
6865 (str - decimal_point - 1) does not make any changes in the test results.
6866
6867 This should be addressed somehow.
6868 Looks like a reminder from before real DECIMAL times.
6869 */
6870 28 return (uint)(str - decimal_point);
6871 }
6872
6873 /**
6874 This function is only called during parsing:
6875 - when parsing SQL query from sql_yacc.yy
6876 - when parsing XPath query from item_xmlfunc.cc
6877 We will signal an error if value is not a true double value (overflow):
6878 eng: Illegal %s '%-.192s' value found during parsing
6879
6880 Note: str_arg does not necessarily have to be a null terminated string,
6881 e.g. it is NOT when called from item_xmlfunc.cc or sql_yacc.yy.
6882 */
6883
6884 7128 void Item_float::init(const char *str_arg, uint length) {
6885 int error;
6886 const char *end_not_used;
6887
1/2
✓ Branch 0 taken 7128 times.
✗ Branch 1 not taken.
7128 value = my_strntod(&my_charset_bin, str_arg, length, &end_not_used, &error);
6888
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7120 times.
7128 if (error) {
6889 char tmp[NAME_LEN + 1];
6890 8 snprintf(tmp, sizeof(tmp), "%.*s", length, str_arg);
6891
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 my_error(ER_ILLEGAL_VALUE_FOR_TYPE, MYF(0), "double", tmp);
6892 }
6893
1/2
✓ Branch 0 taken 7128 times.
✗ Branch 1 not taken.
7128 presentation.copy(str_arg, length);
6894
1/2
✓ Branch 0 taken 7128 times.
✗ Branch 1 not taken.
7128 item_name.copy(str_arg, length);
6895 7128 set_data_type(MYSQL_TYPE_DOUBLE);
6896 7128 decimals = (uint8)nr_of_decimals(str_arg, str_arg + length);
6897 7128 max_length = length;
6898 7128 fixed = true;
6899 7128 }
6900
6901 3285 type_conversion_status Item_float::save_in_field_inner(Field *field, bool) {
6902 3285 double nr = val_real();
6903 // TODO: call set_field_to_null_with_conversions below
6904
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3285 times.
3285 if (null_value) return set_field_to_null(field);
6905 3285 field->set_notnull();
6906 3285 return field->store(nr);
6907 }
6908
6909 836 void Item_float::print(const THD *, String *str,
6910 enum_query_type query_type) const {
6911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 836 times.
836 if (query_type & QT_NORMALIZED_FORMAT) {
6912 str->append("?");
6913 318 return;
6914 }
6915
2/2
✓ Branch 0 taken 318 times.
✓ Branch 1 taken 518 times.
836 if (presentation.ptr()) {
6916
1/2
✓ Branch 0 taken 318 times.
✗ Branch 1 not taken.
318 str->append(presentation.ptr());
6917 318 return;
6918 }
6919 char buffer[20];
6920 518 String num(buffer, sizeof(buffer), &my_charset_bin);
6921
1/2
✓ Branch 0 taken 518 times.
✗ Branch 1 not taken.
518 num.set_real(value, decimals, &my_charset_bin);
6922
1/2
✓ Branch 0 taken 518 times.
✗ Branch 1 not taken.
518 str->append(num);
6923 518 }
6924
6925 /*
6926 hex item
6927 In string context this is a binary string.
6928 In number context this is a longlong value.
6929 */
6930
6931 821 bool Item_float::eq(const Item *arg, bool) const {
6932
5/6
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 785 times.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 36 times.
✓ Branch 5 taken 785 times.
821 if (arg->basic_const_item() && arg->type() == type()) {
6933 /*
6934 We need to cast off const to call val_int(). This should be OK for
6935 a basic constant.
6936 */
6937 36 Item *item = const_cast<Item *>(arg);
6938 36 return item->val_real() == value;
6939 }
6940 785 return false;
6941 }
6942
6943 255632 inline uint char_val(char X) {
6944
4/6
✓ Branch 0 taken 255632 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 165631 times.
✓ Branch 3 taken 90001 times.
✓ Branch 4 taken 90001 times.
✗ Branch 5 not taken.
345633 return (uint)(X >= '0' && X <= '9'
6945 165631 ? X - '0'
6946
2/2
✓ Branch 0 taken 56992 times.
✓ Branch 1 taken 33009 times.
345633 : X >= 'A' && X <= 'Z' ? X - 'A' + 10 : X - 'a' + 10);
6947 }
6948
6949 Item_hex_string::Item_hex_string() { hex_string_init("", 0); }
6950
6951 1 Item_hex_string::Item_hex_string(const char *str, uint str_length) {
6952
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 hex_string_init(str, str_length);
6953 1 }
6954
6955 21071 Item_hex_string::Item_hex_string(const POS &pos, const LEX_STRING &literal)
6956 21071 : super(pos) {
6957
1/2
✓ Branch 0 taken 21071 times.
✗ Branch 1 not taken.
21071 hex_string_init(literal.str, literal.length);
6958 21071 }
6959
6960 32211 LEX_CSTRING Item_hex_string::make_hex_str(const char *str, size_t str_length) {
6961 32211 size_t max_length = (str_length + 1) / 2;
6962
2/4
✓ Branch 0 taken 32211 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32211 times.
✗ Branch 3 not taken.
32211 char *ptr = (char *)(*THR_MALLOC)->Alloc(max_length + 1);
6963
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32211 times.
32211 if (ptr == nullptr) return NULL_CSTR;
6964 32211 LEX_CSTRING ret = {ptr, max_length};
6965 32211 char *end = ptr + max_length;
6966
2/2
✓ Branch 0 taken 466 times.
✓ Branch 1 taken 31745 times.
32211 if (max_length * 2 != str_length)
6967 466 *ptr++ = char_val(*str++); // Not even, assume 0 prefix
6968
2/2
✓ Branch 0 taken 127583 times.
✓ Branch 1 taken 32211 times.
159794 while (ptr != end) {
6969 127583 *ptr++ = (char)(char_val(str[0]) * 16 + char_val(str[1]));
6970 127583 str += 2;
6971 }
6972 32211 *ptr = 0; // needed if printed in error message
6973 32211 return ret;
6974 }
6975
6976 243 uint Item_hex_string::decimal_precision() const {
6977
6/9
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 208 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 8 times.
✓ Branch 8 taken 3 times.
243 switch (max_length) {
6978 2 case 0:
6979 2 return count_digits(0U);
6980 208 case 1:
6981 208 return count_digits(0xFFU);
6982 15 case 2:
6983 15 return count_digits(0xFFFFU);
6984 case 3:
6985 return count_digits(0xFFFFFFU);
6986 7 case 4:
6987 7 return count_digits(0xFFFFFFFFU);
6988 case 5:
6989 return count_digits(0xFFFFFFFFFFU);
6990 case 6:
6991 return count_digits(0xFFFFFFFFFFFFU);
6992 8 case 7:
6993 8 return count_digits(0xFFFFFFFFFFFFFFU);
6994 3 default:
6995 // val_int() and val_decimal() look at the first eight bytes. Longer
6996 // values are truncated.
6997
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 assert(max_length >= 8);
6998 3 return count_digits(0xFFFFFFFFFFFFFFFFU);
6999 }
7000 }
7001
7002 21072 void Item_hex_string::hex_string_init(const char *str, uint str_length) {
7003
1/2
✓ Branch 0 taken 21072 times.
✗ Branch 1 not taken.
21072 LEX_CSTRING s = make_hex_str(str, str_length);
7004
1/2
✓ Branch 0 taken 21072 times.
✗ Branch 1 not taken.
21072 str_value.set(s.str, s.length, &my_charset_bin);
7005 21072 set_data_type(MYSQL_TYPE_VARCHAR);
7006 21072 max_length = s.length;
7007 21072 collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
7008 21072 fixed = true;
7009 21072 unsigned_flag = true;
7010 21072 }
7011
7012 11977 longlong Item_hex_string::val_int() {
7013 // following assert is redundant, because fixed=1 assigned in constructor
7014
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11977 times.
11977 assert(fixed == 1);
7015 11977 const char *end = str_value.ptr() + str_value.length();
7016 const char *ptr;
7017
7018
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 11965 times.
11977 if (str_value.length() > sizeof(longlong)) {
7019 /*
7020 Too many bytes for longlong; lost bytes are [start, lost_end[ ; there is
7021 no loss of data in conversion only if they are all zeroes.
7022 */
7023 12 const char *lost_end = end - sizeof(longlong);
7024
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 for (ptr = str_value.ptr(); ptr < lost_end; ++ptr)
7025
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (*ptr != 0) {
7026 // Human-readable, size-limited printout of the hex:
7027 12 char errbuff[MYSQL_ERRMSG_SIZE], *errptr = errbuff;
7028 12 *errptr++ = 'x';
7029 12 *errptr++ = '\'';
7030
2/2
✓ Branch 0 taken 273 times.
✓ Branch 1 taken 12 times.
285 for (ptr = str_value.ptr(); ptr < end; ++ptr) {
7031
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 273 times.
273 if (errptr > errbuff + sizeof(errbuff) - 4) break;
7032 273 *errptr++ = _dig_vec_lower[((uchar)*ptr) >> 4];
7033 273 *errptr++ = _dig_vec_lower[((uchar)*ptr) & 0x0F];
7034 }
7035 12 *errptr++ = '\'';
7036 12 *errptr++ = 0;
7037
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 THD *thd = current_thd;
7038
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 push_warning_printf(
7039 thd, Sql_condition::SL_WARNING, ER_TRUNCATED_WRONG_VALUE,
7040 ER_THD(thd, ER_TRUNCATED_WRONG_VALUE), "BINARY", errbuff);
7041 12 return 0;
7042 }
7043 }
7044
7045 11965 ptr = end - str_value.length();
7046 11965 ulonglong value = 0;
7047
2/2
✓ Branch 0 taken 26729 times.
✓ Branch 1 taken 11965 times.
38694 for (; ptr != end; ptr++) value = (value << 8) + (ulonglong)(uchar)*ptr;
7048 11965 return (longlong)value;
7049 }
7050
7051 54 my_decimal *Item_hex_string::val_decimal(my_decimal *decimal_value) {
7052 // following assert is redundant, because fixed=1 assigned in constructor
7053
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 assert(fixed == 1);
7054 54 ulonglong value = (ulonglong)val_int();
7055 54 int2my_decimal(E_DEC_FATAL_ERROR, value, true, decimal_value);
7056 54 return (decimal_value);
7057 }
7058
7059 23411 type_conversion_status Item_hex_string::save_in_field_inner(Field *field,
7060 bool) {
7061 23411 field->set_notnull();
7062
2/2
✓ Branch 0 taken 17061 times.
✓ Branch 1 taken 6350 times.
23411 if (field->result_type() == STRING_RESULT)
7063 17061 return field->store(str_value.ptr(), str_value.length(),
7064 17061 collation.collation);
7065
7066 ulonglong nr;
7067 6350 size_t length = str_value.length();
7068
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6346 times.
6350 if (!length) {
7069 4 field->reset();
7070 4 return TYPE_WARN_OUT_OF_RANGE;
7071 }
7072
2/2
✓ Branch 0 taken 672 times.
✓ Branch 1 taken 5674 times.
6346 if (length > 8) {
7073
2/2
✓ Branch 0 taken 654 times.
✓ Branch 1 taken 18 times.
672 nr = field->is_flag_set(UNSIGNED_FLAG) ? ULLONG_MAX : LLONG_MAX;
7074 672 goto warn;
7075 }
7076 5674 nr = (ulonglong)val_int();
7077
8/8
✓ Branch 0 taken 715 times.
✓ Branch 1 taken 4959 times.
✓ Branch 2 taken 110 times.
✓ Branch 3 taken 605 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 98 times.
✓ Branch 6 taken 12 times.
✓ Branch 7 taken 5662 times.
5674 if ((length == 8) && !field->is_flag_set(UNSIGNED_FLAG) && (nr > LLONG_MAX)) {
7078 12 nr = LLONG_MAX;
7079 12 goto warn;
7080 }
7081 5662 return field->store((longlong)nr, true); // Assume hex numbers are unsigned
7082
7083 684 warn:
7084 684 const type_conversion_status res = field->store((longlong)nr, true);
7085
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 632 times.
684 if (res == TYPE_OK)
7086 52 field->set_warning(Sql_condition::SL_WARNING, ER_WARN_DATA_OUT_OF_RANGE, 1);
7087 684 return res;
7088 }
7089
7090 129 void Item_hex_string::print(const THD *, String *str,
7091 enum_query_type query_type) const {
7092
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 127 times.
129 if (query_type & QT_NORMALIZED_FORMAT) {
7093
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 str->append("?");
7094 2 return;
7095 }
7096 127 const uchar *ptr = pointer_cast<const uchar *>(str_value.ptr());
7097 127 const uchar *end = ptr + str_value.length();
7098
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 str->append("0x");
7099
2/2
✓ Branch 0 taken 964 times.
✓ Branch 1 taken 127 times.
1091 for (; ptr != end; ptr++) {
7100 964 str->append(_dig_vec_lower[*ptr >> 4]);
7101 964 str->append(_dig_vec_lower[*ptr & 0x0F]);
7102 }
7103 }
7104
7105 540 bool Item_hex_string::eq(const Item *item, bool binary_cmp) const {
7106
5/6
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 515 times.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 25 times.
✓ Branch 5 taken 515 times.
540 if (item->basic_const_item() && item->type() == type()) {
7107 // Should be OK for a basic constant.
7108 25 Item *arg = const_cast<Item *>(item);
7109 25 String str;
7110
3/6
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 25 times.
✗ Branch 5 not taken.
25 if (binary_cmp) return !stringcmp(&str_value, arg->val_str(&str));
7111 return !sortcmp(&str_value, arg->val_str(&str), collation.collation);
7112 25 }
7113 515 return false;
7114 }
7115
7116 79 Item *Item_hex_string::safe_charset_converter(THD *, const CHARSET_INFO *tocs) {
7117
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 String tmp, *str = val_str(&tmp);
7118
7119
2/4
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 79 times.
✗ Branch 3 not taken.
79 auto conv = new Item_string(str->ptr(), str->length(), tocs);
7120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 if (conv == nullptr) return nullptr;
7121 79 conv->mark_result_as_const();
7122 79 return conv;
7123 79 }
7124
7125 /*
7126 bin item.
7127 In string context this is a binary string.
7128 In number context this is a longlong value.
7129 */
7130
7131 6189 LEX_CSTRING Item_bin_string::make_bin_str(const char *str, size_t str_length) {
7132 6189 const char *end = str + str_length - 1;
7133 6189 uchar bits = 0;
7134 6189 uint power = 1;
7135
7136 6189 size_t max_length = (str_length + 7) >> 3;
7137
2/4
✓ Branch 0 taken 6189 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6189 times.
✗ Branch 3 not taken.
6189 char *ptr = (char *)(*THR_MALLOC)->Alloc(max_length + 1);
7138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6189 times.
6189 if (ptr == nullptr) return NULL_CSTR;
7139
7140 6189 LEX_CSTRING ret{ptr, max_length};
7141
7142
2/2
✓ Branch 0 taken 6183 times.
✓ Branch 1 taken 6 times.
6189 if (max_length > 0) {
7143 6183 ptr += max_length - 1;
7144 6183 ptr[1] = 0; // Set end null for string
7145
2/2
✓ Branch 0 taken 117503 times.
✓ Branch 1 taken 6183 times.
123686 for (; end >= str; end--) {
7146
2/2
✓ Branch 0 taken 11331 times.
✓ Branch 1 taken 106172 times.
117503 if (power == 256) {
7147 11331 power = 1;
7148 11331 *ptr-- = bits;
7149 11331 bits = 0;
7150 }
7151
2/2
✓ Branch 0 taken 60968 times.
✓ Branch 1 taken 56535 times.
117503 if (*end == '1') bits |= power;
7152 117503 power <<= 1;
7153 }
7154 6183 *ptr = (char)bits;
7155 } else
7156 6 ptr[0] = 0;
7157
7158 6189 return ret;
7159 }
7160
7161 6171 void Item_bin_string::bin_string_init(const char *str, size_t str_length) {
7162
1/2
✓ Branch 0 taken 6171 times.
✗ Branch 1 not taken.
6171 LEX_CSTRING s = make_bin_str(str, str_length);
7163 6171 max_length = s.length;
7164
1/2
✓ Branch 0 taken 6171 times.
✗ Branch 1 not taken.
6171 str_value.set(s.str, s.length, &my_charset_bin);
7165 6171 collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
7166 6171 fixed = true;
7167 6171 }
7168
7169 /**
7170 Pack data in buffer for sending.
7171 */
7172
7173 1064944 bool Item_null::send(Protocol *protocol, String *) {
7174 1064944 return protocol->store_null();
7175 }
7176
7177 702 Item_json::Item_json(unique_ptr_destroy_only<Json_wrapper> value,
7178 702 const Item_name_string &name)
7179 702 : m_value(std::move(value)) {
7180 702 set_data_type_json();
7181 702 item_name = name;
7182 702 }
7183
7184 2808 Item_json::~Item_json() = default;
7185
7186 175 void Item_json::print(const THD *, String *str, enum_query_type) const {
7187
1/2
✓ Branch 0 taken 175 times.
✗ Branch 1 not taken.
175 str->append("json'");
7188
1/2
✓ Branch 0 taken 175 times.
✗ Branch 1 not taken.
175 m_value->to_string(str, true, "", JsonDocumentDefaultDepthHandler);
7189
1/2
✓ Branch 0 taken 175 times.
✗ Branch 1 not taken.
175 str->append("'");
7190 175 }
7191
7192 2140 bool Item_json::val_json(Json_wrapper *result) {
7193 2140 *result = *m_value;
7194 2140 return false;
7195 }
7196
7197 /*
7198 The functions below are rarely called, some of them are probably unreachable
7199 from SQL, because Item_json is used in a more limited way than other
7200 subclasses of Item_basic_constant. Most notably, there is no JSON literal
7201 syntax which gets translated into Item_json objects by the parser.
7202 */
7203
7204 1 double Item_json::val_real() { return m_value->coerce_real(item_name.ptr()); }
7205
7206 2 longlong Item_json::val_int() { return m_value->coerce_int(item_name.ptr()); }
7207
7208 2 String *Item_json::val_str(String *str) {
7209 2 str->length(0);
7210
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (m_value->to_string(str, true, item_name.ptr(),
7211 JsonDocumentDefaultDepthHandler))
7212 return error_str();
7213 2 return str;
7214 }
7215
7216 1 my_decimal *Item_json::val_decimal(my_decimal *buf) {
7217 1 return m_value->coerce_decimal(buf, item_name.ptr());
7218 }
7219
7220 1 bool Item_json::get_date(MYSQL_TIME *ltime, my_time_flags_t) {
7221 1 return m_value->coerce_date(ltime, item_name.ptr());
7222 }
7223
7224 1 bool Item_json::get_time(MYSQL_TIME *ltime) {
7225 1 return m_value->coerce_time(ltime, item_name.ptr());
7226 }
7227
7228 1 Item *Item_json::clone_item() const {
7229
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 THD *const thd = current_thd;
7230 auto wr = make_unique_destroy_only<Json_wrapper>(thd->mem_root,
7231
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 m_value->clone_dom());
7232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (wr == nullptr) return nullptr;
7233
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 return new Item_json(std::move(wr), item_name);
7234 1 }
7235
7236 /**
7237 This is only called from items that is not of type item_field.
7238 */
7239
7240 11677624 bool Item::send(Protocol *protocol, String *buffer) {
7241
10/10
✓ Branch 0 taken 8747406 times.
✓ Branch 1 taken 395 times.
✓ Branch 2 taken 479 times.
✓ Branch 3 taken 55929 times.
✓ Branch 4 taken 2562170 times.
✓ Branch 5 taken 373 times.
✓ Branch 6 taken 143265 times.
✓ Branch 7 taken 756 times.
✓ Branch 8 taken 165709 times.
✓ Branch 9 taken 1153 times.
11677624 switch (data_type()) {
7242 8747406 default:
7243 case MYSQL_TYPE_NULL:
7244 case MYSQL_TYPE_BOOL:
7245 case MYSQL_TYPE_INVALID:
7246 case MYSQL_TYPE_DECIMAL:
7247 case MYSQL_TYPE_ENUM:
7248 case MYSQL_TYPE_SET:
7249 case MYSQL_TYPE_TINY_BLOB:
7250 case MYSQL_TYPE_MEDIUM_BLOB:
7251 case MYSQL_TYPE_LONG_BLOB:
7252 case MYSQL_TYPE_BLOB:
7253 case MYSQL_TYPE_GEOMETRY:
7254 case MYSQL_TYPE_STRING:
7255 case MYSQL_TYPE_VAR_STRING:
7256 case MYSQL_TYPE_VARCHAR:
7257 case MYSQL_TYPE_BIT:
7258 case MYSQL_TYPE_NEWDECIMAL:
7259 case MYSQL_TYPE_JSON: {
7260 8747406 const String *res = val_str(buffer);
7261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8747405 times.
8747405 assert(null_value == (res == nullptr));
7262
2/2
✓ Branch 0 taken 8657168 times.
✓ Branch 1 taken 90237 times.
8747405 if (res != nullptr)
7263 8657168 return protocol->store_string(res->ptr(), res->length(),
7264 8657171 res->charset());
7265 90237 break;
7266 }
7267 395 case MYSQL_TYPE_TINY: {
7268 395 longlong nr = val_int();
7269
2/2
✓ Branch 0 taken 227 times.
✓ Branch 1 taken 168 times.
395 if (!null_value) return protocol->store_tiny(nr);
7270 168 break;
7271 }
7272 479 case MYSQL_TYPE_SHORT:
7273 case MYSQL_TYPE_YEAR: {
7274 479 longlong nr = val_int();
7275
2/2
✓ Branch 0 taken 291 times.
✓ Branch 1 taken 188 times.
479 if (!null_value) return protocol->store_short(nr);
7276 188 break;
7277 }
7278 55929 case MYSQL_TYPE_INT24:
7279 case MYSQL_TYPE_LONG: {
7280 55929 longlong nr = val_int();
7281
2/2
✓ Branch 0 taken 38335 times.
✓ Branch 1 taken 17594 times.
55929 if (!null_value) return protocol->store_long(nr);
7282 17594 break;
7283 }
7284 2562170 case MYSQL_TYPE_LONGLONG: {
7285 2562170 longlong nr = val_int();
7286
2/2
✓ Branch 0 taken 2236926 times.
✓ Branch 1 taken 325218 times.
2562144 if (!null_value) return protocol->store_longlong(nr, unsigned_flag);
7287 325218 break;
7288 }
7289 373 case MYSQL_TYPE_FLOAT: {
7290 373 float nr = static_cast<float>(val_real());
7291
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 285 times.
373 if (!null_value) return protocol->store_float(nr, decimals, 0);
7292 285 break;
7293 }
7294 143265 case MYSQL_TYPE_DOUBLE: {
7295 143265 double nr = val_real();
7296
2/2
✓ Branch 0 taken 137647 times.
✓ Branch 1 taken 5618 times.
143265 if (!null_value) return protocol->store_double(nr, decimals, 0);
7297 5618 break;
7298 }
7299 756 case MYSQL_TYPE_DATE: {
7300 MYSQL_TIME tm;
7301
1/2
✓ Branch 0 taken 756 times.
✗ Branch 1 not taken.
756 get_date(&tm, TIME_FUZZY_DATE);
7302
3/4
✓ Branch 0 taken 460 times.
✓ Branch 1 taken 296 times.
✓ Branch 2 taken 460 times.
✗ Branch 3 not taken.
756 if (!null_value) return protocol->store_date(tm);
7303 296 break;
7304 }
7305 165709 case MYSQL_TYPE_DATETIME:
7306 case MYSQL_TYPE_TIMESTAMP: {
7307 MYSQL_TIME tm;
7308
1/2
✓ Branch 0 taken 165709 times.
✗ Branch 1 not taken.
165709 get_date(&tm, TIME_FUZZY_DATE);
7309
3/4
✓ Branch 0 taken 25470 times.
✓ Branch 1 taken 140239 times.
✓ Branch 2 taken 25470 times.
✗ Branch 3 not taken.
165709 if (!null_value) return protocol->store_datetime(tm, decimals);
7310 140239 break;
7311 }
7312 1153 case MYSQL_TYPE_TIME: {
7313 MYSQL_TIME tm;
7314
1/2
✓ Branch 0 taken 1153 times.
✗ Branch 1 not taken.
1153 get_time(&tm);
7315
3/4
✓ Branch 0 taken 930 times.
✓ Branch 1 taken 223 times.
✓ Branch 2 taken 930 times.
✗ Branch 3 not taken.
1153 if (!null_value) return protocol->store_time(tm, decimals);
7316 223 break;
7317 }
7318 }
7319
7320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 580066 times.
580066 assert(null_value);
7321 580066 return protocol->store_null();
7322 }
7323
7324 15880954 bool Item::update_null_value() {
7325 char buff[STRING_BUFFER_USUAL_SIZE];
7326 15880954 String str(buff, sizeof(buff), collation.collation);
7327
2/4
✓ Branch 0 taken 15880954 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15880954 times.
✗ Branch 3 not taken.
31761908 return evaluate(current_thd, &str);
7328 15880954 }
7329
7330 /**
7331 Evaluate item, possibly using the supplied buffer
7332
7333 @param thd Thread context
7334 @param buffer Buffer, in case item needs a large one
7335
7336 @returns false if success, true if error
7337 */
7338
7339 15895407 bool Item::evaluate(THD *thd, String *buffer) {
7340
7/8
✗ Branch 0 not taken.
✓ Branch 1 taken 500 times.
✓ Branch 2 taken 26524 times.
✓ Branch 3 taken 15844943 times.
✓ Branch 4 taken 3378 times.
✓ Branch 5 taken 10572 times.
✓ Branch 6 taken 8019 times.
✓ Branch 7 taken 1471 times.
15895407 switch (data_type()) {
7341 case MYSQL_TYPE_INVALID:
7342 default:
7343 assert(false);
7344 (void)val_str(buffer);
7345 break;
7346 500 case MYSQL_TYPE_JSON: {
7347 500 Json_wrapper wr;
7348
1/2
✓ Branch 0 taken 500 times.
✗ Branch 1 not taken.
500 (void)val_json(&wr);
7349 500 } break;
7350 26524 case MYSQL_TYPE_NULL:
7351 case MYSQL_TYPE_DECIMAL:
7352 case MYSQL_TYPE_ENUM:
7353 case MYSQL_TYPE_SET:
7354 case MYSQL_TYPE_TINY_BLOB:
7355 case MYSQL_TYPE_MEDIUM_BLOB:
7356 case MYSQL_TYPE_LONG_BLOB:
7357 case MYSQL_TYPE_BLOB:
7358 case MYSQL_TYPE_GEOMETRY:
7359 case MYSQL_TYPE_STRING:
7360 case MYSQL_TYPE_VAR_STRING:
7361 case MYSQL_TYPE_VARCHAR:
7362 case MYSQL_TYPE_BIT: {
7363 26524 (void)val_str(buffer);
7364 26524 break;
7365 }
7366 15844943 case MYSQL_TYPE_BOOL:
7367 case MYSQL_TYPE_TINY:
7368 case MYSQL_TYPE_SHORT:
7369 case MYSQL_TYPE_YEAR:
7370 case MYSQL_TYPE_INT24:
7371 case MYSQL_TYPE_LONG:
7372 case MYSQL_TYPE_LONGLONG: {
7373 15844943 (void)val_int();
7374 15844943 break;
7375 }
7376 3378 case MYSQL_TYPE_NEWDECIMAL: {
7377 3378 my_decimal decimal_value;
7378
1/2
✓ Branch 0 taken 3378 times.
✗ Branch 1 not taken.
3378 (void)val_decimal(&decimal_value);
7379 3378 break;
7380 3378 }
7381
7382 10572 case MYSQL_TYPE_FLOAT:
7383 case MYSQL_TYPE_DOUBLE: {
7384 10572 (void)val_real();
7385 10572 break;
7386 }
7387 8019 case MYSQL_TYPE_DATETIME:
7388 case MYSQL_TYPE_DATE:
7389 case MYSQL_TYPE_TIMESTAMP: {
7390 MYSQL_TIME tm;
7391
1/2
✓ Branch 0 taken 8019 times.
✗ Branch 1 not taken.
8019 (void)get_date(&tm, TIME_FUZZY_DATE);
7392 8019 break;
7393 }
7394 1471 case MYSQL_TYPE_TIME: {
7395 MYSQL_TIME tm;
7396
1/2
✓ Branch 0 taken 1471 times.
✗ Branch 1 not taken.
1471 (void)get_time(&tm);
7397 1471 break;
7398 }
7399 }
7400 15895407 const bool result = thd->is_error();
7401 // Convention: set NULL value indicator on error
7402
2/2
✓ Branch 0 taken 933 times.
✓ Branch 1 taken 15894474 times.
15895407 if (result) null_value = true;
7403 15895407 return result;
7404 }
7405
7406 /**
7407 Check if an item is a constant one and can be cached.
7408
7409 @param [out] arg If != NULL <=> Cache this item.
7410
7411 @return true Go deeper in item tree.
7412 @return false Don't go deeper in item tree.
7413 */
7414
7415 10456748 bool Item::cache_const_expr_analyzer(uchar **arg) {
7416 10456748 cache_const_expr_arg *carg = (cache_const_expr_arg *)*arg;
7417
2/2
✓ Branch 0 taken 10353329 times.
✓ Branch 1 taken 103419 times.
10456748 if (!carg->cache_item) {
7418 10353329 Item *item = real_item();
7419 /*
7420 Cache constant items unless it's a basic constant, a constant field,
7421 a subquery (they use their own cache),
7422 a ROW object (rollback logic can get messy),
7423 or it is already cached.
7424 */
7425
4/4
✓ Branch 0 taken 2581119 times.
✓ Branch 1 taken 7772414 times.
✓ Branch 2 taken 90772 times.
✓ Branch 3 taken 10262623 times.
12934489 if (const_for_execution() &&
7426
3/4
✓ Branch 0 taken 105751 times.
✓ Branch 1 taken 2475347 times.
✓ Branch 2 taken 105751 times.
✗ Branch 3 not taken.
2581119 !(basic_const_item() || item->basic_const_item() ||
7427
4/4
✓ Branch 0 taken 103952 times.
✓ Branch 1 taken 1799 times.
✓ Branch 2 taken 92802 times.
✓ Branch 3 taken 11150 times.
105751 item->type() == Item::FIELD_ITEM || item->type() == SUBSELECT_ITEM ||
7428
4/4
✓ Branch 0 taken 92474 times.
✓ Branch 1 taken 328 times.
✓ Branch 2 taken 92389 times.
✓ Branch 3 taken 85 times.
92802 item->type() == ROW_ITEM || item->type() == CACHE_ITEM ||
7429
2/2
✓ Branch 0 taken 90772 times.
✓ Branch 1 taken 1500 times.
92389 item->type() == PARAM_ITEM))
7430 /*
7431 Note that we use cache_item as a flag (NULL vs non-NULL), but we
7432 are storing the pointer so that we can assert that we cache the
7433 correct item in Item::cache_const_expr_transformer().
7434 */
7435 90772 carg->cache_item = this;
7436 /*
7437 JSON functions can read JSON from strings or use SQL scalars by
7438 converting them to JSON scalars. Such conversion takes time and on
7439 repetitive calls result is significant performance penalty.
7440
7441 Check if such data can be cached:
7442 1) this item is constant
7443 2) this item is an arg to a function
7444 3) it's a source of JSON data
7445 4) this item's type isn't JSON so conversion will be required
7446 5) it's not cached already
7447
7448 Difference with the block above is that this one caches any const item,
7449 because the goal here is to avoid conversion, rather than re-evaluation.
7450 */
7451 10262623 else if (const_for_execution() && // 1
7452
5/6
✓ Branch 0 taken 2490342 times.
✓ Branch 1 taken 7772385 times.
✓ Branch 2 taken 2490358 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2484779 times.
✓ Branch 5 taken 7777972 times.
12753109 carg->stack.elements > 0 &&
7453
2/2
✓ Branch 0 taken 2484780 times.
✓ Branch 1 taken 5602 times.
2490358 carg->stack.head()->type() == FUNC_ITEM) // 2
7454 {
7455 2484779 Item_func *head = down_cast<Item_func *>(carg->stack.head());
7456 enum_const_item_cache what_cache;
7457
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 611 times.
2485432 if ((what_cache = head->can_cache_json_arg(this)) && // 3
7458
4/4
✓ Branch 0 taken 635 times.
✓ Branch 1 taken 2484130 times.
✓ Branch 2 taken 35 times.
✓ Branch 3 taken 2484741 times.
2485411 data_type() != MYSQL_TYPE_JSON && // 4
7459
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
24 item->type() != CACHE_ITEM) // 5
7460 {
7461 35 carg->cache_item = this;
7462 35 carg->cache_arg = what_cache;
7463 }
7464 }
7465 // Push only if we're going down the tree, so transformer will pop the item
7466 10353520 carg->stack.push_front(item);
7467 /*
7468 If this item will be cached, no need to explore items further down
7469 in the tree, but the transformer must be called, so return 'true'.
7470 If this item will not be cached, items further down in the tree
7471 must be explored, so return 'true'.
7472 */
7473 10353467 return true;
7474 }
7475 /*
7476 An item above in the tree is to be cached, so need to cache the present
7477 item, and no need to go down the tree.
7478 */
7479 103419 return false;
7480 }
7481
7482 24992 bool Item::can_be_substituted_for_gc(bool array) const {
7483
3/3
✓ Branch 0 taken 10411 times.
✓ Branch 1 taken 8561 times.
✓ Branch 2 taken 6020 times.
24992 switch (real_item()->type()) {
7484 10411 case FUNC_ITEM:
7485 case COND_ITEM:
7486 10411 return true;
7487 8561 case FIELD_ITEM:
7488 // Fields can be substituted with a generated column for a multi-valued
7489 // index defined on the field. Otherwise, for non-arrays, we don't
7490 // substitute fields with generated columns, since functional indexes
7491 // cannot be defined on a plain column, only on expressions.
7492 8561 return array;
7493 6020 default:
7494 6020 return false;
7495 }
7496 }
7497
7498 /**
7499 Set the maximum number of characters required by any of the items in args.
7500 */
7501 5904082 void Item::aggregate_char_length(Item **args, uint nitems) {
7502 5904082 uint32 char_length = 0;
7503 /*
7504 To account for character sets with different number of bytes per character,
7505 set char_length equal to max_length if the aggregated character set is
7506 binary to prevent truncation of data as some characters require more than
7507 one byte.
7508 */
7509 5904082 bool bin_charset = collation.collation == &my_charset_bin;
7510
2/2
✓ Branch 0 taken 13328425 times.
✓ Branch 1 taken 5904082 times.
19232507 for (uint i = 0; i < nitems; i++)
7511
2/2
✓ Branch 0 taken 1339 times.
✓ Branch 1 taken 13327086 times.
26655511 char_length = max(char_length, bin_charset ? args[i]->max_length
7512
1/2
✓ Branch 0 taken 13327086 times.
✗ Branch 1 not taken.
13327086 : args[i]->max_char_length());
7513
2/2
✓ Branch 0 taken 5691971 times.
✓ Branch 1 taken 212111 times.
5904082 if (char_length * collation.collation->mbmaxlen > max_length)
7514 5691971 fix_char_length(char_length);
7515 5904082 }
7516
7517 /**
7518 Set max_length and decimals of function if function is floating point and
7519 result length/precision depends on argument ones.
7520
7521 @param item Argument array.
7522 @param nitems Number of arguments in the array.
7523 */
7524 159730 void Item::aggregate_float_properties(Item **item, uint nitems) {
7525
2/4
✓ Branch 0 taken 159730 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 159730 times.
159730 assert(result_type() == REAL_RESULT);
7526 159730 uint32 length = 0;
7527 159730 uint8 decimals_cnt = 0;
7528 159730 uint32 maxl = 0;
7529
2/2
✓ Branch 0 taken 319370 times.
✓ Branch 1 taken 159730 times.
479100 for (uint i = 0; i < nitems; i++) {
7530
2/2
✓ Branch 0 taken 203756 times.
✓ Branch 1 taken 115614 times.
319370 if (decimals_cnt != DECIMAL_NOT_SPECIFIED) {
7531 203756 decimals_cnt = max(decimals_cnt, item[i]->decimals);
7532 203756 length = max(length, (item[i]->max_length - item[i]->decimals));
7533 }
7534 319370 maxl = max(maxl, item[i]->max_length);
7535 }
7536
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 159522 times.
159730 if (decimals_cnt != DECIMAL_NOT_SPECIFIED) {
7537 208 maxl = length;
7538 208 length += decimals_cnt;
7539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208 times.
208 if (length < maxl) // If previous operation gave overflow
7540 maxl = UINT_MAX32;
7541 else
7542 208 maxl = length;
7543 }
7544
7545 159730 this->max_length = maxl;
7546 159730 this->decimals = decimals_cnt;
7547 159730 }
7548
7549 /**
7550 Set precision and decimals of function when this depends on arguments'
7551 values for these quantities.
7552
7553 @param item Argument array.
7554 @param nitems Number of arguments in the array.
7555 */
7556 1923189 void Item::aggregate_decimal_properties(Item **item, uint nitems) {
7557
2/4
✓ Branch 0 taken 1923189 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1923189 times.
1923189 assert(result_type() == DECIMAL_RESULT);
7558 1923189 int max_int_part = 0;
7559 1923189 uint8 decimal_cnt = 0;
7560
2/2
✓ Branch 0 taken 3973699 times.
✓ Branch 1 taken 1923189 times.
5896888 for (uint i = 0; i < nitems; i++) {
7561 3973699 decimal_cnt = max(decimal_cnt, item[i]->decimals);
7562
1/2
✓ Branch 0 taken 3973699 times.
✗ Branch 1 not taken.
3973699 max_int_part = max(max_int_part, item[i]->decimal_int_part());
7563 }
7564 1923189 int precision = min(max_int_part + decimal_cnt, DECIMAL_MAX_PRECISION);
7565
1/2
✓ Branch 0 taken 1923189 times.
✗ Branch 1 not taken.
1923189 set_data_type_decimal(precision, decimal_cnt);
7566 1923189 }
7567
7568 /**
7569 Set fractional seconds precision for temporal functions.
7570
7571 @param item Argument array
7572 @param nitems Number of arguments in the array.
7573 */
7574 173684 void Item::aggregate_temporal_properties(Item **item, uint nitems) {
7575
2/4
✓ Branch 0 taken 173684 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 173684 times.
173684 assert(result_type() == STRING_RESULT);
7576 173684 uint8 decimal_cnt = 0;
7577
7578
5/7
✓ Branch 0 taken 173434 times.
✓ Branch 1 taken 75 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 93 times.
✓ Branch 4 taken 80 times.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
173684 switch (data_type()) {
7579 173434 case MYSQL_TYPE_DATETIME:
7580
2/2
✓ Branch 0 taken 346861 times.
✓ Branch 1 taken 173434 times.
520295 for (uint i = 0; i < nitems; i++)
7581
1/2
✓ Branch 0 taken 346861 times.
✗ Branch 1 not taken.
346861 decimal_cnt = max(decimal_cnt, uint8(item[i]->datetime_precision()));
7582 173434 decimal_cnt = min(decimal_cnt, uint8(DATETIME_MAX_DECIMALS));
7583 173434 set_data_type_datetime(decimal_cnt);
7584 173434 break;
7585
7586 75 case MYSQL_TYPE_TIMESTAMP:
7587
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 75 times.
224 for (uint i = 0; i < nitems; i++)
7588
1/2
✓ Branch 0 taken 149 times.
✗ Branch 1 not taken.
149 decimal_cnt = max(decimal_cnt, uint8(item[i]->datetime_precision()));
7589 75 decimal_cnt = min(decimal_cnt, uint8(DATETIME_MAX_DECIMALS));
7590 75 set_data_type_timestamp(decimal_cnt);
7591 75 break;
7592
7593 case MYSQL_TYPE_NEWDATE:
7594 assert(false);
7595 set_data_type_date();
7596 set_data_type(MYSQL_TYPE_NEWDATE);
7597 break;
7598
7599 93 case MYSQL_TYPE_DATE:
7600 93 set_data_type_date();
7601 93 break;
7602
7603 80 case MYSQL_TYPE_TIME:
7604
2/2
✓ Branch 0 taken 158 times.
✓ Branch 1 taken 80 times.
238 for (uint i = 0; i < nitems; i++)
7605
1/2
✓ Branch 0 taken 158 times.
✗ Branch 1 not taken.
158 decimal_cnt = max(decimal_cnt, uint8(item[i]->time_precision()));
7606 80 decimal_cnt = min(decimal_cnt, uint8(DATETIME_MAX_DECIMALS));
7607 80 set_data_type_time(decimal_cnt);
7608 80 break;
7609
7610 2 case MYSQL_TYPE_YEAR:
7611
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 set_data_type_year();
7612 2 break;
7613
7614 default:
7615 assert(false); /* purecov: inspected */
7616 }
7617 173684 }
7618
7619 /**
7620 Aggregate string properties (character set, collation and maximum length) for
7621 string function.
7622
7623 @param name Name of function
7624 @param items Argument array.
7625 @param nitems Number of arguments.
7626
7627 @retval False on success, true on error.
7628 */
7629 4134655 bool Item::aggregate_string_properties(const char *name, Item **items,
7630 uint nitems) {
7631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4134655 times.
4134655 assert(result_type() == STRING_RESULT);
7632
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 4134644 times.
4134655 if (agg_item_charsets_for_string_result(collation, name, items, nitems, 1))
7633 11 return true;
7634
2/2
✓ Branch 0 taken 173684 times.
✓ Branch 1 taken 3960960 times.
4134644 if (is_temporal_type(data_type())) {
7635 /*
7636 aggregate_temporal_properties() will set collation to numeric, causing
7637 the character set to be explicitly set to latin1, which may not match the
7638 aggregated character set. The collation must therefore be restored after
7639 the temporal properties have been computed.
7640 */
7641 173684 auto aggregated_collation = collation;
7642
1/2
✓ Branch 0 taken 173684 times.
✗ Branch 1 not taken.
173684 aggregate_temporal_properties(items, nitems);
7643 173684 collation.set(aggregated_collation);
7644 /*
7645 Set max_length again as the aggregated character set may have different
7646 number of bytes per character than latin1.
7647 */
7648 173684 fix_char_length(max_length);
7649 } else
7650 3960960 decimals = min(decimals, uint8(DECIMAL_NOT_SPECIFIED));
7651 4134644 aggregate_char_length(items, nitems);
7652
7653 /*
7654 If the resulting data type is a fixed length character or binary string
7655 and the result maximum length in characters is longer than the MySQL
7656 maximum CHAR/BINARY size, convert to a variable-sized type.
7657 */
7658
3/4
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 4134294 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4134644 times.
4134994 if (data_type() == MYSQL_TYPE_STRING &&
7659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 350 times.
350 max_char_length() > MAX_FIELD_CHARLENGTH)
7660 set_data_type(MYSQL_TYPE_VARCHAR);
7661
7662 4134644 return false;
7663 }
7664
7665 /**
7666 This function is used to resolve type for numeric result type of CASE,
7667 COALESCE, IF and LEAD/LAG. COALESCE is a CASE abbreviation according to the
7668 standard.
7669
7670 @param result_type The desired result type
7671 @param item The arguments of func
7672 @param nitems The number of arguments
7673 */
7674 3692843 void Item::aggregate_num_type(Item_result result_type, Item **item,
7675 uint nitems) {
7676 3692843 collation.set_numeric();
7677
3/4
✓ Branch 0 taken 1923189 times.
✓ Branch 1 taken 216 times.
✓ Branch 2 taken 1769438 times.
✗ Branch 3 not taken.
3692843 switch (result_type) {
7678 1923189 case DECIMAL_RESULT:
7679 1923189 aggregate_decimal_properties(item, nitems);
7680 1923189 break;
7681 216 case REAL_RESULT:
7682 216 aggregate_float_properties(item, nitems);
7683 216 break;
7684 1769438 case INT_RESULT:
7685 case STRING_RESULT:
7686 1769438 aggregate_char_length(item, nitems);
7687 1769438 decimals = 0;
7688 1769438 break;
7689 case ROW_RESULT:
7690 default:
7691 assert(0);
7692 }
7693 3692843 }
7694
7695 /**
7696 Cache item if needed.
7697
7698 @param arg Descriptor of what and how to cache @see cache_const_expr_arg
7699
7700 @return cache if cache needed.
7701 @return this otherwise.
7702 */
7703
7704 10353350 Item *Item::cache_const_expr_transformer(uchar *arg) {
7705 10353350 cache_const_expr_arg *carg = (cache_const_expr_arg *)arg;
7706 10353350 carg->stack.pop();
7707
2/2
✓ Branch 0 taken 90807 times.
✓ Branch 1 taken 10262655 times.
10353462 if (carg->cache_item) // Item is to be cached, note that it is used as a flag
7708 {
7709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90807 times.
90807 assert(carg->cache_item == this);
7710 Item_cache *cache;
7711 /*
7712 Flag applies to present item, must reset it so it does not affect
7713 the parent item.
7714 */
7715 90807 carg->cache_item = nullptr;
7716 // Cache arg of a JSON function to avoid repetitive conversion
7717
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 90772 times.
90807 if (carg->cache_arg != CACHE_NONE) {
7718 35 Item *itm = this;
7719 35 Item_func *caller = down_cast<Item_func *>(carg->stack.head());
7720 35 String buf;
7721 35 Json_wrapper wr;
7722 35 enum_const_item_cache what_cache = carg->cache_arg;
7723
7724 35 carg->cache_arg = CACHE_NONE;
7725
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 14 times.
35 if (what_cache == CACHE_JSON_VALUE) {
7726 // Cache parse result of JSON string
7727
6/8
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 20 times.
41 if (get_json_wrapper(&itm, 0, &buf, caller->func_name(), &wr) ||
7728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 null_value) {
7729
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 return current_thd->is_error() ? nullptr : this;
7730 }
7731 } else {
7732 // Cache SQL scalar converted to JSON
7733
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 assert(what_cache == CACHE_JSON_ATOM);
7734 14 String conv_buf;
7735
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 if (get_json_atom_wrapper(&itm, 0, caller->func_name(), &buf, &conv_buf,
7736
3/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 12 times.
28 &wr, nullptr, true) ||
7737
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
14 null_value) {
7738
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
2 return current_thd->is_error() ? nullptr : this;
7739 }
7740
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
14 }
7741 // Should've been checked at get_*_wrapper()
7742
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 32 times.
32 assert(wr.type() != enum_json_type::J_ERROR);
7743
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
64 Item_cache_json *jcache = new Item_cache_json();
7744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!jcache) return nullptr;
7745
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 jcache->setup(this);
7746
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 jcache->store_value(this, &wr);
7747 32 cache = jcache;
7748
4/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 32 times.
✓ Branch 3 taken 3 times.
38 } else {
7749 90772 cache = Item_cache::get_cache(this);
7750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90772 times.
90772 if (!cache) return nullptr;
7751 90772 cache->setup(this);
7752 90772 cache->store(this);
7753 }
7754 /*
7755 This item is cached - for subqueries this effectively means that they
7756 are optimized away.
7757 */
7758 90804 mark_subqueries_optimized_away();
7759 90804 return cache;
7760 }
7761 10262655 return this;
7762 }
7763
7764 383655917 bool Item_field::send(Protocol *protocol, String *) {
7765 383655917 return protocol->store_field(field);
7766 }
7767
7768 /**
7769 Add the field to the select list and substitute it for the reference to
7770 the field.
7771
7772 @details
7773 If the field doesn't belong to the table being inserted into then it is
7774 added to the select list, pointer to it is stored in the ref_item_array
7775 of the select and the field itself is substituted for the Item_ref object.
7776 This is done in order to get correct values from update fields that
7777 belongs to the SELECT part in the INSERT .. SELECT .. ON DUPLICATE KEY
7778 UPDATE statement.
7779
7780 @retval nullptr if an error occurred
7781 @retval ref if all conditions are met
7782 @retval this field otherwise
7783 */
7784
7785 676 Item *Item_field::update_value_transformer(uchar *select_arg) {
7786 676 Query_block *select = pointer_cast<Query_block *>(select_arg);
7787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 676 times.
676 assert(fixed);
7788
7789
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 676 times.
676 assert((table_ref == select->context.table_list) ==
7790 (field->table == select->context.table_list->table));
7791
4/4
✓ Branch 0 taken 572 times.
✓ Branch 1 taken 104 times.
✓ Branch 2 taken 569 times.
✓ Branch 3 taken 107 times.
1248 if (field->table != select->context.table_list->table &&
7792
2/2
✓ Branch 0 taken 569 times.
✓ Branch 1 taken 3 times.
572 type() != Item::TRIGGER_FIELD_ITEM) {
7793 569 Item **tmp = select->add_hidden_item(this);
7794
2/4
✓ Branch 0 taken 569 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 569 times.
✗ Branch 3 not taken.
569 return new Item_ref(&select->context, tmp, db_name, table_name, field_name);
7795 }
7796 107 return this;
7797 }
7798
7799 4382952 void Item_field::print(const THD *thd, String *str,
7800 enum_query_type query_type) const {
7801
6/6
✓ Branch 0 taken 4367908 times.
✓ Branch 1 taken 15044 times.
✓ Branch 2 taken 198 times.
✓ Branch 3 taken 4367710 times.
✓ Branch 4 taken 198 times.
✓ Branch 5 taken 4382754 times.
4382952 if (field && field->is_field_for_functional_index()) {
7802 198 field->gcol_info->expr_item->print(thd, str, query_type);
7803 198 return;
7804 }
7805
7806
6/6
✓ Branch 0 taken 4367710 times.
✓ Branch 1 taken 15044 times.
✓ Branch 2 taken 4367099 times.
✓ Branch 3 taken 611 times.
✓ Branch 4 taken 2405 times.
✓ Branch 5 taken 4364694 times.
4382754 if (field && field->table && field->table->const_table &&
7807
2/2
✓ Branch 0 taken 2341 times.
✓ Branch 1 taken 64 times.
2405 !(query_type & QT_NO_DATA_EXPANSION)) {
7808 char buff[MAX_FIELD_WIDTH];
7809 2341 String tmp(buff, sizeof(buff), str->charset());
7810
1/2
✓ Branch 0 taken 2341 times.
✗ Branch 1 not taken.
2341 field->val_str(&tmp);
7811
3/4
✓ Branch 0 taken 2341 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1002 times.
✓ Branch 3 taken 1339 times.
2341 if (field->is_null())
7812
1/2
✓ Branch 0 taken 1002 times.
✗ Branch 1 not taken.
1002 str->append("NULL");
7813 else {
7814
1/2
✓ Branch 0 taken 1339 times.
✗ Branch 1 not taken.
1339 str->append('\'');
7815
1/2
✓ Branch 0 taken 1339 times.
✗ Branch 1 not taken.
1339 str->append(tmp);
7816
1/2
✓ Branch 0 taken 1339 times.
✗ Branch 1 not taken.
1339 str->append('\'');
7817 }
7818 2341 return;
7819 2341 }
7820 4380413 Item_ident::print(thd, str, query_type);
7821 }
7822
7823 /**
7824 Calculate condition filtering effect for "WHERE field", which
7825 implicitly means "WHERE field <> 0". The filtering effect is
7826 therefore identical to that of Item_func_ne.
7827 */
7828 float Item_field::get_filtering_effect(THD *, table_map filter_for_table,
7829 table_map,
7830 const MY_BITMAP *fields_to_ignore,
7831 double rows_in_table) {
7832 if (used_tables() != filter_for_table ||
7833 bitmap_is_set(fields_to_ignore, field->field_index()))
7834 return COND_FILTER_ALLPASS;
7835
7836 return 1.0f - get_cond_filter_default_probability(rows_in_table,
7837 COND_FILTER_EQUALITY);
7838 }
7839
7840 103099531 float Item_field::get_cond_filter_default_probability(
7841 double max_distinct_values, float default_filter) const {
7842
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103099531 times.
103099531 assert(max_distinct_values >= 1.0);
7843
7844 // Some field types have a limited number of possible values
7845
3/3
✓ Branch 0 taken 690961 times.
✓ Branch 1 taken 2987 times.
✓ Branch 2 taken 102405662 times.
103099531 switch (field->real_type()) {
7846 690961 case MYSQL_TYPE_ENUM: {
7847 // ENUM can only have the values defined in the typelib
7848 690961 const uint enum_values = static_cast<Field_enum *>(field)->typelib->count;
7849 690961 max_distinct_values =
7850 690961 min(static_cast<double>(enum_values), max_distinct_values);
7851 690961 break;
7852 }
7853 2987 case MYSQL_TYPE_BIT: {
7854 // BIT(N) can have no more than 2^N distinct values
7855 2987 const uint bits = static_cast<Field_bit *>(field)->field_length;
7856 2987 const double combos = pow(2.0, (int)bits);
7857 2987 max_distinct_values = min(combos, max_distinct_values);
7858 2958 break;
7859 }
7860 102405662 default:
7861 102405662 break;
7862 }
7863 103099581 return max(static_cast<float>(1 / max_distinct_values), default_filter);
7864 }
7865
7866 4360452 Item_ref::Item_ref(Name_resolution_context *context_arg, Item **item,
7867 const char *db_name_arg, const char *table_name_arg,
7868 4360452 const char *field_name_arg, bool alias_of_expr_arg)
7869 : Item_ident(context_arg, db_name_arg, table_name_arg, field_name_arg),
7870 4360452 ref(item) {
7871 4360452 m_alias_of_expr = alias_of_expr_arg;
7872 /*
7873 This constructor used to create some internals references over fixed items
7874 */
7875
6/8
✓ Branch 0 taken 4359579 times.
✓ Branch 1 taken 873 times.
✓ Branch 2 taken 4359379 times.
✓ Branch 3 taken 200 times.
✓ Branch 4 taken 4359379 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4359379 times.
✗ Branch 7 not taken.
4360452 if (ref && *ref && (*ref)->fixed) set_properties();
7876 4360452 }
7877
7878 38399 Item_ref::Item_ref(Name_resolution_context *context_arg, Item **item,
7879 38399 const char *field_name_arg)
7880 38399 : Item_ident(context_arg, "", "", field_name_arg), ref(item) {
7881
2/4
✓ Branch 0 taken 38399 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38399 times.
✗ Branch 3 not taken.
38399 assert(ref && *ref);
7882
2/4
✓ Branch 0 taken 38399 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38399 times.
✗ Branch 3 not taken.
38399 if ((*ref)->fixed) set_properties();
7883 38399 }
7884
7885 /**
7886 Resolve the name of a reference to a column reference.
7887
7888 The method resolves the column reference represented by 'this' as a column
7889 present in one of: GROUP BY clause, SELECT clause, outer queries. It is
7890 used typically for columns in the HAVING clause which are not under
7891 aggregate functions.
7892
7893 POSTCONDITION @n
7894 Item_ref::ref is 0 or points to a valid item.
7895
7896 @note
7897 The name resolution algorithm used is (where [T_j] is an optional table
7898 name that qualifies the column name):
7899
7900 @code
7901 resolve_extended([T_j].col_ref_i)
7902 {
7903 Search for a column or derived column named col_ref_i [in table T_j]
7904 in the SELECT and GROUP clauses of Q.
7905
7906 if such a column is NOT found AND // Lookup in outer queries.
7907 there are outer queries
7908 {
7909 for each outer query Q_k beginning from the inner-most one
7910 {
7911 Search for a column or derived column named col_ref_i
7912 [in table T_j] in the SELECT and GROUP clauses of Q_k.
7913
7914 if such a column is not found AND
7915 - Q_k is not a group query AND
7916 - Q_k is not inside an aggregate function
7917 OR
7918 - Q_(k-1) is not in a HAVING or SELECT clause of Q_k
7919 {
7920 search for a column or derived column named col_ref_i
7921 [in table T_j] in the FROM clause of Q_k;
7922 }
7923 }
7924 }
7925 }
7926 @endcode
7927 @n
7928 This procedure treats GROUP BY and SELECT clauses as one namespace for
7929 column references in HAVING. Notice that compared to
7930 Item_field::fix_fields, here we first search the SELECT and GROUP BY
7931 clauses, and then we search the FROM clause.
7932
7933 @param[in] thd current thread
7934 @param[in,out] reference view column if this item was resolved to a
7935 view column
7936
7937 @todo
7938 Here we could first find the field anyway, and then test this
7939 condition, so that we can give a better error message -
7940 ER_WRONG_FIELD_WITH_GROUP, instead of the less informative
7941 ER_BAD_FIELD_ERROR which we produce now.
7942
7943 @retval
7944 true if error
7945 @retval
7946 false on success
7947 */
7948
7949 4563 bool Item_ref::fix_fields(THD *thd, Item **reference) {
7950
1/2
✓ Branch 0 taken 4563 times.
✗ Branch 1 not taken.
4563 DBUG_TRACE;
7951
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4563 times.
4563 assert(fixed == 0);
7952
7953 Internal_error_handler_holder<View_error_handler, TABLE_LIST> view_handler(
7954
1/2
✓ Branch 0 taken 4563 times.
✗ Branch 1 not taken.
4563 thd, context->view_error_handler, context->view_error_handler_arg);
7955
7956
3/4
✓ Branch 0 taken 1073 times.
✓ Branch 1 taken 3490 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1073 times.
4563 if (!ref || ref == not_found_item) {
7957
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3490 times.
3490 assert(context->query_block == thd->lex->current_query_block());
7958 3490 if (!(ref =
7959
3/4
✓ Branch 0 taken 3490 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 3486 times.
3490 resolve_ref_in_select_and_group(thd, this, context->query_block)))
7960 4 goto error; /* Some error occurred (e.g. ambiguous names). */
7961
7962
2/2
✓ Branch 0 taken 464 times.
✓ Branch 1 taken 3022 times.
3486 if (ref == not_found_item) /* This reference was not resolved. */
7963 {
7964 464 Name_resolution_context *last_checked_context = context;
7965 464 Name_resolution_context *outer_context = context->outer_context;
7966 Field *from_field;
7967 464 ref = nullptr;
7968
7969
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 446 times.
464 if (!outer_context) {
7970 /* The current reference cannot be resolved in this query. */
7971
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 my_error(ER_BAD_FIELD_ERROR, MYF(0), this->full_name(),
7972
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
18 current_thd->where);
7973 18 goto error;
7974 }
7975
7976 /*
7977 If there is an outer context (select), try to
7978 resolve this reference in the outer select(s).
7979
7980 We treat each subselect as a separate namespace, so that different
7981 subselects may contain columns with the same names. The subselects are
7982 searched starting from the innermost.
7983 */
7984 446 from_field = not_found_field;
7985
7986 446 Query_block *cur_query_block = context->query_block;
7987
7988 do {
7989 533 Query_block *select = outer_context->query_block;
7990 533 last_checked_context = outer_context;
7991 533 Query_expression *cur_query_expression = nullptr;
7992 533 enum_parsing_context place = CTX_NONE;
7993
7994 // See comments and similar loop in Item_field::fix_outer_field()
7995 while (true) {
7996
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 645 times.
645 if (!cur_query_block) goto loop;
7997
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 645 times.
645 assert(cur_query_block != select);
7998 645 cur_query_expression = cur_query_block->master_query_expression();
7999
2/2
✓ Branch 0 taken 533 times.
✓ Branch 1 taken 112 times.
645 if (cur_query_expression->outer_query_block() == select) break;
8000
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 cur_query_expression->accumulate_used_tables(OUTER_REF_TABLE_BIT);
8001 112 cur_query_block = cur_query_expression->outer_query_block();
8002 }
8003
8004
1/2
✓ Branch 0 taken 533 times.
✗ Branch 1 not taken.
533 place = cur_query_expression->place();
8005
8006
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 525 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
533 if (place == CTX_DERIVED && select->end_lateral_table == nullptr)
8007 goto loop;
8008
8009 /* Search in the SELECT and GROUP lists of the outer select. */
8010
4/4
✓ Branch 0 taken 388 times.
✓ Branch 1 taken 145 times.
✓ Branch 2 taken 388 times.
✓ Branch 3 taken 145 times.
921 if (select_alias_referencable(place) &&
8011
1/2
✓ Branch 0 taken 388 times.
✗ Branch 1 not taken.
388 outer_context->resolve_in_select_list) {
8012
3/4
✓ Branch 0 taken 388 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 361 times.
388 if (!(ref = resolve_ref_in_select_and_group(thd, this, select)))
8013 27 goto error; /* Some error occurred (e.g. ambiguous names). */
8014
2/2
✓ Branch 0 taken 261 times.
✓ Branch 1 taken 100 times.
361 if (ref != not_found_item) {
8015
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261 times.
261 assert((*ref)->fixed);
8016
2/4
✓ Branch 0 taken 261 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 261 times.
✗ Branch 3 not taken.
261 cur_query_expression->accumulate_used_tables((*ref)->used_tables());
8017 261 break;
8018 }
8019 /*
8020 Set ref to 0 to ensure that we get an error in case we replaced
8021 this item with another item and still use this item in some
8022 other place of the parse tree.
8023 */
8024 100 ref = nullptr;
8025 }
8026
8027 /*
8028 Check table fields only if the subquery is used in a context that
8029 is not the HAVING clause, or in case the HAVING clause can be
8030 implemented as a WHERE clause (i.e. the query block is not grouped
8031 - implicitly or explicitly - and DISTINCT filtering is not present).
8032 TODO:
8033 Implement proper SQL resolving, by looking at fields from columns
8034 only and reject fields in HAVING clause that are not functionally
8035 dependent on grouping columns from this query block.
8036 In order to preserve MySQL semantics, we may need to accept
8037 fields from the SELECT fields, until this feature has been removed.
8038 */
8039
4/4
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 224 times.
✓ Branch 2 taken 242 times.
✓ Branch 3 taken 3 times.
266 if (place != CTX_HAVING ||
8040
4/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 1 times.
21 (!select->with_sum_func && select->group_list.elements == 0 &&
8041
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1 times.
19 !select->is_distinct())) {
8042 /*
8043 In case of view, find_field_in_tables() write pointer to view
8044 field expression to 'reference', i.e. it substitute that
8045 expression instead of this Item_ref
8046 */
8047
1/2
✓ Branch 0 taken 242 times.
✗ Branch 1 not taken.
242 from_field = find_field_in_tables(
8048 thd, this, outer_context->first_name_resolution_table,
8049 outer_context->last_name_resolution_table, reference,
8050 IGNORE_EXCEPT_NON_UNIQUE, thd->want_privilege, true);
8051
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 241 times.
242 if (!from_field) goto error;
8052
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 238 times.
241 if (from_field == view_ref_found) {
8053
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Item::Type refer_type = (*reference)->type();
8054
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 cur_query_expression->accumulate_used_tables(
8055
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 (*reference)->used_tables());
8056
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 assert((*reference)->type() == REF_ITEM);
8057
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 mark_as_dependent(
8058
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 thd, last_checked_context->query_block, context->query_block,
8059 this,
8060 ((refer_type == REF_ITEM || refer_type == FIELD_ITEM)
8061 ? (Item_ident *)(*reference)
8062 : nullptr));
8063 /*
8064 view reference found, we substituted it instead of this
8065 Item, so can quit
8066 */
8067 3 return false;
8068 }
8069
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 115 times.
238 if (from_field != not_found_field) {
8070
2/4
✓ Branch 0 taken 123 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 123 times.
✗ Branch 3 not taken.
123 if (cached_table && cached_table->query_block &&
8071
1/2
✓ Branch 0 taken 123 times.
✗ Branch 1 not taken.
123 outer_context->query_block &&
8072
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 123 times.
123 cached_table->query_block != outer_context->query_block) {
8073 /*
8074 Due to cache, find_field_in_tables() can return field which
8075 doesn't belong to provided outer_context. In this case we have
8076 to find proper field context in order to fix field correctly.
8077 */
8078 do {
8079 outer_context = outer_context->outer_context;
8080 select = outer_context->query_block;
8081 cur_query_expression = last_checked_context->query_block
8082 ->master_query_expression();
8083 last_checked_context = outer_context;
8084 } while (outer_context && outer_context->query_block &&
8085 cached_table->query_block != outer_context->query_block);
8086 place = cur_query_expression->place();
8087 }
8088
1/2
✓ Branch 0 taken 123 times.
✗ Branch 1 not taken.
123 cur_query_expression->accumulate_used_tables(
8089 123 from_field->table->pos_in_table_list->map());
8090 123 break;
8091 }
8092 }
8093
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 118 times.
118 assert(from_field == not_found_field);
8094
8095 /* Reference is not found => depend on outer (or just error). */
8096
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 cur_query_expression->accumulate_used_tables(OUTER_REF_TABLE_BIT);
8097
8098 118 loop:
8099 118 outer_context = outer_context->outer_context;
8100
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 31 times.
118 } while (outer_context);
8101
8102
2/4
✓ Branch 0 taken 415 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 415 times.
✗ Branch 3 not taken.
415 assert(from_field != nullptr && from_field != view_ref_found);
8103
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 292 times.
415 if (from_field != not_found_field) {
8104 Item_field *fld;
8105
8106 {
8107
1/2
✓ Branch 0 taken 123 times.
✗ Branch 1 not taken.
123 Prepared_stmt_arena_holder ps_arena_holder(thd);
8108 123 fld = new Item_field(
8109
2/4
✓ Branch 0 taken 123 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 123 times.
✗ Branch 3 not taken.
123 thd, context, from_field->table->pos_in_table_list, from_field);
8110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 123 times.
123 if (fld == nullptr) goto error;
8111
1/2
✓ Branch 0 taken 123 times.
✗ Branch 1 not taken.
123 }
8112
8113 123 *reference = fld;
8114 // WL#6570 remove-after-qa
8115
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 122 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
123 assert(thd->stmt_arena->is_regular() || !thd->lex->is_exec_started());
8116 123 mark_as_dependent(thd, last_checked_context->query_block,
8117
1/2
✓ Branch 0 taken 123 times.
✗ Branch 1 not taken.
123 context->query_block, this, fld);
8118 /*
8119 A reference is resolved to a nest level that's outer or the same as
8120 the nest level of the enclosing set function : adjust the value of
8121 max_aggr_level for the function if it's needed.
8122 */
8123
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 114 times.
123 if (thd->lex->in_sum_func &&
8124 9 thd->lex->in_sum_func->base_query_block->nest_level >=
8125
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 last_checked_context->query_block->nest_level)
8126 9 thd->lex->in_sum_func->max_aggr_level =
8127 18 max(thd->lex->in_sum_func->max_aggr_level,
8128 9 int8(last_checked_context->query_block->nest_level));
8129 123 return false;
8130 }
8131
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 261 times.
292 if (ref == nullptr) {
8132 /* The item was not a table field and not a reference */
8133
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 my_error(ER_BAD_FIELD_ERROR, MYF(0), this->full_name(),
8134
2/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
31 current_thd->where);
8135 31 goto error;
8136 }
8137 /* Should be checked in resolve_ref_in_select_and_group(). */
8138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261 times.
261 assert((*ref)->fixed);
8139 261 mark_as_dependent(thd, last_checked_context->query_block,
8140
1/2
✓ Branch 0 taken 261 times.
✗ Branch 1 not taken.
261 context->query_block, this, this);
8141 /*
8142 A reference is resolved to a nest level that's outer or the same as
8143 the nest level of the enclosing set function : adjust the value of
8144 max_aggr_level for the function if it's needed.
8145 */
8146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 261 times.
261 if (thd->lex->in_sum_func &&
8147 thd->lex->in_sum_func->base_query_block->nest_level >=
8148 last_checked_context->query_block->nest_level)
8149 thd->lex->in_sum_func->max_aggr_level =
8150 max(thd->lex->in_sum_func->max_aggr_level,
8151 int8(last_checked_context->query_block->nest_level));
8152 }
8153 }
8154
8155 // The reference should be fixed at this point.
8156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4356 times.
4356 assert((*ref)->fixed);
8157
8158 /*
8159 Reject invalid references to aggregates.
8160
8161 1) We only accept references to aggregates in a HAVING clause.
8162 (This restriction is not strictly necessary, but we don't want to
8163 lift it without making sure that such queries are handled
8164 correctly. Lifting the restriction will make bugs such as
8165 bug#13633829 and bug#22588319 (aka bug#80116) affect a larger set
8166 of queries.)
8167
8168 2) An aggregate cannot be referenced from the GROUP BY clause of
8169 the query block where the aggregation happens, since grouping
8170 happens before aggregation.
8171 */
8172 4356 if (((*ref)->has_aggregation() &&
8173
6/6
✓ Branch 0 taken 447 times.
✓ Branch 1 taken 3909 times.
✓ Branch 2 taken 412 times.
✓ Branch 3 taken 35 times.
✓ Branch 4 taken 44 times.
✓ Branch 5 taken 4312 times.
8677 !thd->lex->current_query_block()->having_fix_field) || // 1
8174
3/4
✓ Branch 0 taken 4321 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 4312 times.
4321 walk(&Item::has_aggregate_ref_in_group_by, // 2
8175 enum_walk::SUBQUERY_POSTFIX, nullptr)) {
8176
2/4
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
44 my_error(ER_ILLEGAL_REFERENCE, MYF(0), full_name(),
8177 "reference to group function");
8178 44 goto error;
8179 }
8180
8181
1/2
✓ Branch 0 taken 4312 times.
✗ Branch 1 not taken.
4312 set_properties();
8182
8183
2/4
✓ Branch 0 taken 4312 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4312 times.
4312 if ((*ref)->check_cols(1)) goto error;
8184 4312 return false;
8185
8186 125 error:
8187 125 return true;
8188 4563 }
8189
8190 4402963 void Item_ref::set_properties() {
8191
1/2
✓ Branch 0 taken 4402963 times.
✗ Branch 1 not taken.
4402963 DBUG_TRACE;
8192
8193 4402963 set_data_type((*ref)->data_type());
8194 4402963 max_length = (*ref)->max_length;
8195 4402963 set_nullable((*ref)->is_nullable());
8196 4402963 decimals = (*ref)->decimals;
8197 4402963 collation.set((*ref)->collation);
8198 /*
8199 We have to remember if we refer to a sum function, to ensure that
8200 split_sum_func() doesn't try to change the reference.
8201 */
8202 4402963 set_accum_properties(*ref);
8203 4402963 unsigned_flag = (*ref)->unsigned_flag;
8204 4402963 fixed = true;
8205
5/8
✓ Branch 0 taken 4402963 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2506926 times.
✓ Branch 3 taken 1896037 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2506926 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 4402963 times.
6909889 if ((*ref)->type() == FIELD_ITEM &&
8206 2506926 ((Item_ident *)(*ref))->is_alias_of_expr())
8207 set_alias_of_expr();
8208 4402963 }
8209
8210 4486778 void Item_ref::cleanup() {
8211
1/2
✓ Branch 0 taken 4486822 times.
✗ Branch 1 not taken.
4486778 DBUG_TRACE;
8212 4486822 Item_ident::cleanup();
8213 4486821 result_field = nullptr;
8214 4486821 }
8215
8216 /**
8217 Transform an Item_ref object with a transformer callback function.
8218
8219 The function first applies the transform function to the item
8220 referenced by this Item_ref object. If this replaces the item with a
8221 new one, this item object is returned as the result of the
8222 transform. Otherwise the transform function is applied to the
8223 Item_ref object itself.
8224 */
8225
8226 246 Item *Item_ref::transform(Item_transformer transformer, uchar *arg) {
8227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
246 assert((*ref) != nullptr);
8228
8229 /* Transform the object we are referencing. */
8230 246 Item *new_item = (*ref)->transform(transformer, arg);
8231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
246 if (new_item == nullptr) return nullptr;
8232
8233 /*
8234 If the object is transformed into a new object, discard the Item_ref
8235 object and return the new object as result.
8236 */
8237
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 237 times.
246 if (new_item != *ref) return new_item;
8238
8239 /* Transform the item ref object. */
8240
1/2
✓ Branch 0 taken 237 times.
✗ Branch 1 not taken.
237 Item *transformed_item = (this->*transformer)(arg);
8241 // assert(transformed_item == this);
8242 237 return transformed_item;
8243 }
8244
8245 /**
8246 Compile an Item_ref object with a processor and a transformer
8247 callback function.
8248
8249 First the function applies the analyzer to the Item_ref
8250 object. Second it applies the compile function to the object the
8251 Item_ref object is referencing. If this replaces the item with a new
8252 one, this object is returned as the result of the compile.
8253 Otherwise we apply the transformer to the Item_ref object itself.
8254 */
8255
8256 780221 Item *Item_ref::compile(Item_analyzer analyzer, uchar **arg_p,
8257 Item_transformer transformer, uchar *arg_t) {
8258
4/4
✓ Branch 0 taken 519713 times.
✓ Branch 1 taken 260508 times.
✓ Branch 2 taken 234540 times.
✓ Branch 3 taken 545681 times.
780221 if (!(this->*analyzer)(arg_p)) return this;
8259
8260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 545681 times.
545681 assert((*ref) != nullptr);
8261 545681 Item *new_item = (*ref)->compile(analyzer, arg_p, transformer, arg_t);
8262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 545681 times.
545681 if (new_item == nullptr) return nullptr;
8263
8264 /*
8265 If the object is compiled into a new object, discard the Item_ref
8266 object and return the new object as result.
8267 */
8268
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 545570 times.
545681 if (new_item != *ref) return new_item;
8269
8270
2/2
✓ Branch 0 taken 280297 times.
✓ Branch 1 taken 265273 times.
545570 return (this->*transformer)(arg_t);
8271 }
8272
8273 17069 void Item_ref::print(const THD *thd, String *str,
8274 enum_query_type query_type) const {
8275
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 17061 times.
17069 if (ref == nullptr) // Unresolved reference: print reference
8276 8 return Item_ident::print(thd, str, query_type);
8277
8278
1/2
✓ Branch 0 taken 176 times.
✗ Branch 1 not taken.
176 if (m_alias_of_expr && (*ref)->type() != Item::CACHE_ITEM &&
8279
7/10
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 16885 times.
✓ Branch 2 taken 176 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 176 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 176 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 176 times.
✓ Branch 9 taken 16885 times.
17237 ref_type() != VIEW_REF && table_name == nullptr && item_name.ptr()) {
8280
1/2
✓ Branch 0 taken 176 times.
✗ Branch 1 not taken.
176 Simple_cstring str1 = (*ref)->real_item()->item_name;
8281
1/2
✓ Branch 0 taken 176 times.
✗ Branch 1 not taken.
176 append_identifier(thd, str, str1.ptr(), str1.length());
8282 } else
8283 16885 (*ref)->print(thd, str, query_type);
8284 }
8285
8286 23117512 bool Item_ref::send(Protocol *prot, String *tmp) {
8287 23117512 return (*ref)->send(prot, tmp);
8288 }
8289
8290 63743 double Item_ref::val_real() {
8291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63743 times.
63743 assert(fixed);
8292 63743 double tmp = (*ref)->val_real();
8293 63743 null_value = (*ref)->null_value;
8294 63743 return tmp;
8295 }
8296
8297 546695 longlong Item_ref::val_int() {
8298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 546695 times.
546695 assert(fixed);
8299 546695 longlong tmp = (*ref)->val_int();
8300 546695 null_value = (*ref)->null_value;
8301 546695 return tmp;
8302 }
8303
8304 1801 longlong Item_ref::val_time_temporal() {
8305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1801 times.
1801 assert(fixed);
8306
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1800 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1801 assert((*ref)->is_temporal() || (*ref)->is_null());
8307 1801 longlong tmp = (*ref)->val_time_temporal();
8308 1801 null_value = (*ref)->null_value;
8309 1801 return tmp;
8310 }
8311
8312 8704 longlong Item_ref::val_date_temporal() {
8313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8704 times.
8704 assert(fixed);
8314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8704 times.
8704 assert((*ref)->is_temporal());
8315 8704 longlong tmp = (*ref)->val_date_temporal();
8316 8704 null_value = (*ref)->null_value;
8317 8704 return tmp;
8318 }
8319
8320 1311 bool Item_ref::val_bool() {
8321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1311 times.
1311 assert(fixed);
8322 1311 bool tmp = (*ref)->val_bool();
8323 1311 null_value = (*ref)->null_value;
8324 1311 return tmp;
8325 }
8326
8327 28575776 String *Item_ref::val_str(String *tmp) {
8328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28575776 times.
28575776 assert(fixed);
8329 28575776 tmp = (*ref)->val_str(tmp);
8330 28575776 null_value = (*ref)->null_value;
8331 28575776 return tmp;
8332 }
8333
8334 1645 bool Item_ref::val_json(Json_wrapper *result) {
8335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1645 times.
1645 assert(fixed);
8336 1645 bool ok = (*ref)->val_json(result);
8337 1645 null_value = (*ref)->null_value;
8338 1645 return ok;
8339 }
8340
8341 29138 bool Item_ref::is_null() {
8342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29138 times.
29138 assert(fixed);
8343 29138 bool tmp = (*ref)->is_null();
8344 29138 null_value = (*ref)->null_value;
8345 29138 return tmp;
8346 }
8347
8348 4530 bool Item_ref::get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) {
8349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4530 times.
4530 assert(fixed);
8350 4530 bool result = (*ref)->get_date(ltime, fuzzydate);
8351 4530 null_value = (*ref)->null_value;
8352 4530 return result;
8353 }
8354
8355 32144 my_decimal *Item_ref::val_decimal(my_decimal *decimal_value) {
8356 32144 my_decimal *val = (*ref)->val_decimal(decimal_value);
8357 32144 null_value = (*ref)->null_value;
8358 32144 return val;
8359 }
8360
8361 126084528 type_conversion_status Item_ref::save_in_field_inner(Field *to,
8362 bool no_conversions) {
8363 type_conversion_status res;
8364 126084528 res = (*ref)->save_in_field(to, no_conversions);
8365 126084529 null_value = (*ref)->null_value;
8366 126084529 return res;
8367 }
8368
8369 754238 void Item_ref::make_field(Send_field *field) {
8370 754238 (*ref)->make_field(field);
8371 /* Non-zero in case of a view */
8372
1/2
✓ Branch 0 taken 754238 times.
✗ Branch 1 not taken.
754238 if (item_name.is_set()) field->col_name = item_name.ptr();
8373
2/2
✓ Branch 0 taken 754221 times.
✓ Branch 1 taken 17 times.
754238 if (table_name) field->table_name = table_name;
8374
2/2
✓ Branch 0 taken 749824 times.
✓ Branch 1 taken 4414 times.
754238 if (m_orig_db_name) field->db_name = m_orig_db_name;
8375
1/2
✓ Branch 0 taken 754238 times.
✗ Branch 1 not taken.
754238 if (m_orig_field_name) field->org_col_name = m_orig_field_name;
8376
2/2
✓ Branch 0 taken 754221 times.
✓ Branch 1 taken 17 times.
754238 if (m_orig_table_name) field->org_table_name = m_orig_table_name;
8377 /*
8378 Some connectors expect a schema name that is empty when a view column
8379 is defined over an expression that is not a column reference from a
8380 view or a table. This is used to flag the column as read-only.
8381 */
8382
2/2
✓ Branch 0 taken 232536 times.
✓ Branch 1 taken 521702 times.
754238 if (real_item()->type() != Item::FIELD_ITEM) field->db_name = "";
8383 754238 }
8384
8385 2307772 Item *Item_ref::get_tmp_table_item(THD *thd) {
8386
1/2
✓ Branch 0 taken 2307772 times.
✗ Branch 1 not taken.
2307772 DBUG_TRACE;
8387
2/2
✓ Branch 0 taken 250882 times.
✓ Branch 1 taken 2056890 times.
2307772 if (!result_field) {
8388
1/2
✓ Branch 0 taken 250882 times.
✗ Branch 1 not taken.
250882 Item *result = (*ref)->get_tmp_table_item(thd);
8389 250882 return result;
8390 }
8391
8392
2/4
✓ Branch 0 taken 2056889 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2056886 times.
✗ Branch 3 not taken.
2056890 Item_field *item = new Item_field(result_field);
8393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2056886 times.
2056886 if (item == nullptr) return nullptr;
8394
8395 2056886 item->set_orignal_db_name(m_orig_db_name);
8396 2056886 item->db_name = db_name;
8397 2056886 item->table_name = table_name;
8398
4/6
✓ Branch 0 taken 2056886 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2056885 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1404276 times.
✓ Branch 5 taken 652609 times.
2056886 if (real_item()->type() == Item::FIELD_ITEM)
8399 1404276 item->set_original_table_name(
8400
1/2
✓ Branch 0 taken 1404276 times.
✗ Branch 1 not taken.
1404276 down_cast<Item_field *>(real_item())->original_table_name());
8401
8402 2056889 return item;
8403 2307771 }
8404
8405 277 void Item_ref_null_helper::print(const THD *thd, String *str,
8406 enum_query_type query_type) const {
8407 277 str->append(STRING_WITH_LEN("<ref_null_helper>("));
8408
1/2
✓ Branch 0 taken 277 times.
✗ Branch 1 not taken.
277 if (ref)
8409 277 (*ref)->print(thd, str, query_type);
8410 else
8411 str->append('?');
8412 277 str->append(')');
8413 277 }
8414
8415 180 bool Item_ref::collect_item_field_or_ref_processor(uchar *arg) {
8416 180 auto *info = pointer_cast<Collect_item_fields_or_refs *>(arg);
8417
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 55 times.
180 if (info->is_stopped(this)) return false;
8418
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 if (real_item()->type() == Item::FIELD_ITEM) info->m_items->push_back(this);
8419 55 info->stop_at(this);
8420 55 return false;
8421 }
8422
8423 /**
8424 Prepare referenced field then call usual Item_ref::fix_fields .
8425
8426 @param thd Current session.
8427 @param reference reference on reference where this item stored
8428
8429 @retval
8430 false OK
8431 @retval
8432 true Error
8433 */
8434
8435 bool Item_view_ref::fix_fields(THD *thd, Item **reference) {
8436 assert(*ref); // view field reference must be defined
8437
8438 // (*ref)->check_cols() will be made in Item_ref::fix_fields
8439 if ((*ref)->fixed) {
8440 /*
8441 Underlying Item_field objects may be shared. Make sure that the use
8442 is marked regardless of how many ref items that point to this field.
8443 */
8444 Mark_field mf(thd->mark_used_columns);
8445 (*ref)->walk(&Item::mark_field_in_map, enum_walk::POSTFIX, (uchar *)&mf);
8446 } else {
8447 if ((*ref)->fix_fields(thd, ref)) return true; /* purecov: inspected */
8448 }
8449 if (super::fix_fields(thd, reference)) return true;
8450
8451 if (cached_table->is_inner_table_of_outer_join()) {
8452 set_nullable(true);
8453 first_inner_table = cached_table->any_outer_leaf_table();
8454 }
8455 return false;
8456 }
8457
8458 /**
8459 Prepare referenced outer field then call usual Item_ref::fix_fields
8460
8461 @param thd thread handler
8462 @param reference reference on reference where this item stored
8463
8464 @details
8465 The function serves 3 purposes
8466 - adds field to the current select list
8467 - creates an object to use to reference the item (Item_ref)
8468 - fixes reference (Item_ref object)
8469
8470 If a field isn't already on the select list and the base_ref_items array
8471 is provided then it is added to the all_fields list and the pointer to
8472 it is saved in the base_ref_items array.
8473
8474 When the class is chosen it substitutes the original field in the
8475 Item_outer_ref object.
8476
8477 @returns true if error
8478 */
8479
8480 899 bool Item_outer_ref::fix_fields(THD *thd, Item **reference) {
8481 /* outer_ref->check_cols() will be made in Item_ref::fix_fields */
8482
3/8
✓ Branch 0 taken 899 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 899 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 899 times.
899 if ((*ref) && !(*ref)->fixed && ((*ref)->fix_fields(thd, reference)))
8483 return true;
8484
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 882 times.
899 if (super::fix_fields(thd, reference)) return true;
8485
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 873 times.
882 if (!outer_ref) outer_ref = *ref;
8486
2/2
✓ Branch 0 taken 881 times.
✓ Branch 1 taken 1 times.
882 if ((*ref)->type() == Item::FIELD_ITEM)
8487 881 table_name = ((Item_field *)outer_ref)->table_name;
8488
8489 882 Item *item = outer_ref;
8490 882 Item **item_ref = ref;
8491
8492 /*
8493 TODO: this field item already might be present in the select list.
8494 In this case instead of adding new field item we could use an
8495 existing one. The change will lead to less operations for copying fields,
8496 smaller temporary tables and less data passed through filesort.
8497 */
8498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 882 times.
882 assert(!qualifying->base_ref_items.is_null());
8499
2/2
✓ Branch 0 taken 873 times.
✓ Branch 1 taken 9 times.
882 if (!found_in_select_list) {
8500 /*
8501 Add the field item to the select list of the current select.
8502 If it's needed reset each Item_ref item that refers this field with
8503 a new reference taken from ref_item_array.
8504 */
8505 873 item_ref = qualifying->add_hidden_item(item);
8506 /*
8507 Now the item is in the all_fields list, which elements are used to fill
8508 temporary tables created by the optimizer; thus it will be read and must
8509 be marked as such. Outer references are never written to.
8510 */
8511
1/2
✓ Branch 0 taken 873 times.
✗ Branch 1 not taken.
873 if (item->fixed) {
8512 873 Mark_field mf(MARK_COLUMNS_READ);
8513
1/2
✓ Branch 0 taken 873 times.
✗ Branch 1 not taken.
873 item->walk(&Item::mark_field_in_map, enum_walk::POSTFIX, (uchar *)&mf);
8514 }
8515 }
8516
8517 Item_ref *const new_ref =
8518
2/4
✓ Branch 0 taken 882 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 882 times.
✗ Branch 3 not taken.
882 new Item_ref(context, item_ref, db_name, table_name, field_name);
8519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 882 times.
882 if (new_ref == nullptr) return true; /* purecov: inspected */
8520 882 outer_ref = new_ref;
8521 882 ref = &outer_ref;
8522
8523 882 qualifying->select_list_tables |= item->used_tables();
8524
8525 882 return false;
8526 }
8527
8528 79 void Item_outer_ref::fix_after_pullout(Query_block *parent_query_block,
8529 Query_block *removed_query_block) {
8530 /*
8531 If this assertion holds, we need not call fix_after_pullout() on both
8532 *ref and outer_ref, and Item_ref::fix_after_pullout() is sufficient.
8533 */
8534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 assert(*ref == outer_ref);
8535
8536 79 Item_ref::fix_after_pullout(parent_query_block, removed_query_block);
8537 79 }
8538
8539 15 Item *Item_outer_ref::replace_outer_ref(uchar *arg) {
8540 15 auto *info = pointer_cast<Item_outer_ref *>(arg);
8541
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (this == info) return real_item();
8542 return this;
8543 }
8544
8545 429468 void Item_ref::fix_after_pullout(Query_block *parent_query_block,
8546 Query_block *removed_query_block) {
8547 429468 (*ref)->fix_after_pullout(parent_query_block, removed_query_block);
8548
8549 429468 Item_ident::fix_after_pullout(parent_query_block, removed_query_block);
8550 429468 }
8551
8552 /**
8553 Compare two view column references for equality.
8554
8555 A view column reference is considered equal to another column
8556 reference if the second one is a view column and if both column
8557 references resolve to the same item. It is assumed that both
8558 items are of the same type.
8559
8560 @param item item to compare with
8561
8562 @retval
8563 true Referenced item is equal to given item
8564 @retval
8565 false otherwise
8566 */
8567
8568 614382 bool Item_view_ref::eq(const Item *item, bool) const {
8569
2/2
✓ Branch 0 taken 436840 times.
✓ Branch 1 taken 177542 times.
614382 if (item->type() == REF_ITEM) {
8570 436840 const Item_ref *item_ref = down_cast<const Item_ref *>(item);
8571
1/2
✓ Branch 0 taken 436840 times.
✗ Branch 1 not taken.
436840 if (item_ref->ref_type() == VIEW_REF) {
8572 436840 Item *item_ref_ref = *(item_ref->ref);
8573 436840 return ((*ref)->real_item() == item_ref_ref->real_item());
8574 }
8575 }
8576 177542 return false;
8577 }
8578
8579 68349 longlong Item_view_ref::val_int() {
8580
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 68291 times.
68349 if (has_null_row()) {
8581 58 null_value = true;
8582 58 return 0;
8583 }
8584 68291 return super::val_int();
8585 }
8586
8587 25241 double Item_view_ref::val_real() {
8588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25241 times.
25241 if (has_null_row()) {
8589 null_value = true;
8590 return 0.0;
8591 }
8592 25241 return super::val_real();
8593 }
8594
8595 18135 my_decimal *Item_view_ref::val_decimal(my_decimal *dec) {
8596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18135 times.
18135 if (has_null_row()) {
8597 null_value = true;
8598 return nullptr;
8599 }
8600 18135 return super::val_decimal(dec);
8601 }
8602
8603 28210821 String *Item_view_ref::val_str(String *str) {
8604
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 28210792 times.
28210821 if (has_null_row()) {
8605 29 null_value = true;
8606 29 return nullptr;
8607 }
8608 28210792 return super::val_str(str);
8609 }
8610
8611 16 bool Item_view_ref::val_bool() {
8612
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (has_null_row()) {
8613 null_value = true;
8614 return false;
8615 }
8616 16 return super::val_bool();
8617 }
8618
8619 261 bool Item_view_ref::val_json(Json_wrapper *wr) {
8620
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 259 times.
261 if (has_null_row()) {
8621 2 null_value = true;
8622 2 return false;
8623 }
8624 259 return super::val_json(wr);
8625 }
8626
8627 4816 bool Item_view_ref::is_null() {
8628
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 4770 times.
4816 if (has_null_row()) return true;
8629
8630 4770 return (*ref)->is_null();
8631 }
8632
8633 23117856 bool Item_view_ref::send(Protocol *prot, String *tmp) {
8634
2/2
✓ Branch 0 taken 361 times.
✓ Branch 1 taken 23117495 times.
23117856 if (has_null_row()) return prot->store_null();
8635 23117495 return super::send(prot, tmp);
8636 }
8637
8638 126065081 type_conversion_status Item_view_ref::save_in_field_inner(Field *field,
8639 bool no_conversions) {
8640
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 126064932 times.
126065081 if (has_null_row())
8641 149 return set_field_to_null_with_conversions(field, no_conversions);
8642
8643 126064932 return super::save_in_field_inner(field, no_conversions);
8644 }
8645
8646 56 bool Item_view_ref::collect_item_field_or_view_ref_processor(uchar *arg) {
8647 56 auto *info = pointer_cast<Collect_item_fields_or_view_refs *>(arg);
8648
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 24 times.
56 if (info->is_stopped(this)) return false;
8649 // We collect this view ref
8650 // (1) If its qualifying table is in the transformed query block
8651 // (2) If its underlying field's qualifying table is in the transformed
8652 // query block
8653 // (3) If this view ref is an outer reference dependent on the
8654 // transformed query block
8655 24 Item *item = nullptr;
8656 48 item = (context->query_block == info->m_transformed_block) // 1
8657
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 18 times.
42 ? this
8658 18 : ((real_item()->type() == Item::FIELD_ITEM &&
8659 18 (down_cast<Item_field *>(real_item())->context->query_block ==
8660
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 15 times.
18 info->m_transformed_block)) // 2
8661
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
36 ? this->real_item()
8662 15 : ((depended_from == info->m_transformed_block) // 3
8663
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 12 times.
15 ? this
8664 : nullptr));
8665
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
24 if (item != nullptr) info->m_item_fields_or_view_refs->push_back(item);
8666 24 info->stop_at(this);
8667 24 return false;
8668 }
8669
8670 9 Item *Item_view_ref::replace_item_view_ref(uchar *arg) {
8671 9 auto *info = pointer_cast<Item::Item_view_ref_replacement *>(arg);
8672 9 Item *real_item = Item_ref::real_item();
8673
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (real_item == info->m_target) {
8674 Item_field *new_field =
8675
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 new (current_thd->mem_root) Item_field(info->m_field);
8676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (new_field == nullptr) return nullptr;
8677 // Set correct metadata for the new field incl. any alias.
8678
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 if (orig_name.length() != 0) {
8679 // The one moved to new_derived has its orig_name set
8680 7 new_field->item_name.set(orig_name.ptr());
8681 7 new_field->orig_name.set(orig_name.ptr());
8682 } else {
8683 // this is a duplicated view reference, not touched yet.
8684 2 new_field->item_name.set(item_name.ptr());
8685 2 new_field->orig_name.set(item_name.ptr());
8686 }
8687
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 if (info->m_curr_block == info->m_trans_block) {
8688 6 return new_field;
8689 }
8690
8691 // The is an outer reference, so we cannot reuse transformed query
8692 // block's Item_field; make a new one for this query block
8693 3 new_field->depended_from = info->m_trans_block;
8694 3 new_field->context = &info->m_curr_block->context;
8695 3 return new_field;
8696 }
8697 return this;
8698 }
8699
8700 46 Item *Item_view_ref::replace_view_refs_with_clone(uchar *arg) {
8701 Condition_pushdown::Derived_table_info *dti =
8702 46 pointer_cast<Condition_pushdown::Derived_table_info *>(arg);
8703
8704 // Replace the view ref with a clone to the referenced item.
8705 // We use a different context to resolve the clone from that of
8706 // the derived table context.
8707 // For Ex:
8708 // SELECT * FROM
8709 // (SELECT f1 FROM (SELECT f1 FROM t1 GROUP BY f1) AS dt1) AS dt2
8710 // WHERE f1 > 3 GROUP BY f1;
8711 // Here dt2 gets merged with the outer query block. As a result, "f1"
8712 // in the outer query block (in select list, where clause and group by)
8713 // will be a view reference. The underlying field for all three
8714 // view references is shared. Therefore, when "f1>3" needs to be
8715 // pushed down to dt1, we need to clone the referenced item (dt2.f1).
8716 // Since the query block having dt2 is merged with the outer query
8717 // block, the context to resolve the field will be different than
8718 // the derived table context (dt1).
8719 46 return dti->m_derived_query_block->outer_query_block()->clone_expression(
8720 46 current_thd, *ref,
8721
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 38 times.
54 (dti->m_derived_table->is_system_view ||
8722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 (dti->m_derived_table->referencing_view &&
8723
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
46 dti->m_derived_table->referencing_view->is_system_view)));
8724 }
8725
8726 4575 bool Item_default_value::itemize(Parse_context *pc, Item **res) {
8727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4575 times.
4575 if (skip_itemize(res)) return false;
8728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4575 times.
4575 if (super::itemize(pc, res)) return true;
8729
8730
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 4514 times.
4575 if (arg != nullptr) {
8731
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (arg->itemize(pc, &arg)) return true;
8732
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 60 times.
61 if (arg->is_splocal()) {
8733 1 Item_splocal *il = static_cast<Item_splocal *>(arg);
8734
8735 1 my_error(ER_WRONG_COLUMN_NAME, MYF(0), il->m_name.ptr());
8736 1 return true;
8737 }
8738 }
8739 4574 return false;
8740 }
8741
8742 bool Item_default_value::eq(const Item *item, bool binary_cmp) const {
8743 return item->type() == DEFAULT_VALUE_ITEM &&
8744 down_cast<const Item_default_value *>(item)->arg->eq(arg, binary_cmp);
8745 }
8746
8747 4530 bool Item_default_value::fix_fields(THD *thd, Item **) {
8748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4530 times.
4530 assert(!fixed);
8749
8750 Internal_error_handler_holder<View_error_handler, TABLE_LIST> view_handler(
8751
1/2
✓ Branch 0 taken 4530 times.
✗ Branch 1 not taken.
4530 thd, context->view_error_handler, context->view_error_handler_arg);
8752
2/2
✓ Branch 0 taken 4481 times.
✓ Branch 1 taken 49 times.
4530 if (arg == nullptr) {
8753 4481 fixed = true;
8754 4481 return false;
8755 }
8756
4/8
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 49 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 49 times.
49 if (!arg->fixed && arg->fix_fields(thd, &arg)) return true;
8757
8758
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 Item *const real_arg = arg->real_item();
8759
3/4
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 48 times.
49 if (real_arg->type() != FIELD_ITEM) {
8760
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), arg->item_name.ptr());
8761 1 return true;
8762 }
8763
8764 48 Item_field *const field_arg = down_cast<Item_field *>(real_arg);
8765
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 42 times.
48 if (field_arg->field->is_flag_set(NO_DEFAULT_VALUE_FLAG)) {
8766
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), field_arg->field->field_name);
8767 6 return true;
8768 }
8769
8770
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 38 times.
42 if (field_arg->field->has_insert_default_general_value_expression()) {
8771
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 my_error(ER_DEFAULT_AS_VAL_GENERATED, MYF(0));
8772 4 return true;
8773 }
8774
8775
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 Field *const def_field = field_arg->field->clone(thd->mem_root);
8776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (def_field == nullptr) return true;
8777
8778
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 def_field->move_field_offset(def_field->table->default_values_offset());
8779 38 m_rowbuffer_saved = def_field->table->s->default_values;
8780
8781 // Assign the cloned field as the one to use hereafter
8782
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 set_field(def_field);
8783
8784 // Needs cached_table for some Item traversal functions:
8785 38 cached_table = table_ref;
8786
8787 // Use same field name as the underlying field:
8788
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 assert(field_name == nullptr);
8789 38 field_name = arg->item_name.ptr();
8790
8791 // Always allow a "read" from the default value.
8792
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 field->table->mark_column_used(field, MARK_COLUMNS_READ);
8793
8794 38 return false;
8795 4530 }
8796
8797 3249 void Item_default_value::bind_fields() {
8798
3/4
✓ Branch 0 taken 3249 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3245 times.
✓ Branch 3 taken 4 times.
3249 if (!fixed || arg == nullptr) return;
8799
8800 4 field->move_field_offset(
8801 4 (ptrdiff_t)(field->table->s->default_values - m_rowbuffer_saved));
8802 4 m_rowbuffer_saved = field->table->s->default_values;
8803 // Always allow a "read" from the default value.
8804 4 field->table->mark_column_used(field, MARK_COLUMNS_READ);
8805 }
8806
8807 13 void Item_default_value::print(const THD *thd, String *str,
8808 enum_query_type query_type) const {
8809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!arg) {
8810 str->append(STRING_WITH_LEN("default"));
8811 return;
8812 }
8813 13 str->append(STRING_WITH_LEN("default("));
8814 13 arg->print(thd, str, query_type);
8815 13 str->append(')');
8816 }
8817
8818 4697 type_conversion_status Item_default_value::save_in_field_inner(
8819 Field *field_arg, bool no_conversions) {
8820 4697 THD *thd = current_thd;
8821
2/2
✓ Branch 0 taken 4687 times.
✓ Branch 1 taken 10 times.
4697 if (!arg) {
8822 4687 if ((field_arg->is_flag_set(NO_DEFAULT_VALUE_FLAG) &&
8823
5/6
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 4648 times.
✓ Branch 2 taken 39 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 36 times.
✓ Branch 5 taken 4651 times.
4726 field_arg->m_default_val_expr == nullptr) &&
8824
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 3 times.
39 field_arg->real_type() != MYSQL_TYPE_ENUM) {
8825
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 35 times.
36 if (field_arg->reset()) {
8826 1 my_error(ER_CANT_CREATE_GEOMETRY_OBJECT, MYF(0));
8827 1 return TYPE_ERR_BAD_VALUE;
8828 }
8829
8830
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (context->view_error_handler) {
8831 TABLE_LIST *view = cached_table->top_table();
8832 push_warning_printf(thd, Sql_condition::SL_WARNING,
8833 ER_NO_DEFAULT_FOR_VIEW_FIELD,
8834 ER_THD(thd, ER_NO_DEFAULT_FOR_VIEW_FIELD), view->db,
8835 view->table_name);
8836 } else {
8837 35 push_warning_printf(
8838 thd, Sql_condition::SL_WARNING, ER_NO_DEFAULT_FOR_FIELD,
8839 ER_THD(thd, ER_NO_DEFAULT_FOR_FIELD), field_arg->field_name);
8840 }
8841 35 return TYPE_ERR_BAD_VALUE;
8842 }
8843
8844 // If this DEFAULT's value is actually an expression, mark the columns
8845 // it uses for reading. For inserts where the name is not explicitly
8846 // mentioned, this is set in COPY_INFO::get_function_default_columns
8847
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 4554 times.
4651 if (field_arg->has_insert_default_general_value_expression()) {
8848
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 97 times.
290 for (uint j = 0; j < field_arg->table->s->fields; j++) {
8849
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 142 times.
193 if (bitmap_is_set(&field_arg->m_default_val_expr->base_columns_map,
8850 j)) {
8851 51 bitmap_set_bit(field_arg->table->read_set, j);
8852 }
8853 }
8854 }
8855
8856 4651 field_arg->set_default();
8857 4651 return field_arg->validate_stored_val(current_thd);
8858 }
8859 10 return Item_field::save_in_field_inner(field_arg, no_conversions);
8860 }
8861
8862 4 Item *Item_default_value::transform(Item_transformer transformer, uchar *args) {
8863 /*
8864 If the value of arg is NULL, then this object represents a constant,
8865 so further transformation is unnecessary (and impossible).
8866 */
8867
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 if (arg == nullptr) return this;
8868
8869 1 Item *new_item = arg->transform(transformer, args);
8870
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (new_item == nullptr) return nullptr; /* purecov: inspected */
8871
8872
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 return (this->*transformer)(args);
8873 }
8874
8875 4 bool Item_insert_value::eq(const Item *item, bool binary_cmp) const {
8876
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 return item->type() == INSERT_VALUE_ITEM &&
8877
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
4 (down_cast<const Item_insert_value *>(item))->arg->eq(arg, binary_cmp);
8878 }
8879
8880 171 bool Item_insert_value::fix_fields(THD *thd, Item **reference) {
8881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 171 times.
171 assert(!fixed);
8882 // Argument must be resolved from first table
8883
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 102 times.
171 if (!arg->fixed) {
8884 69 TABLE_LIST *orig_next_table = context->last_name_resolution_table;
8885 69 context->last_name_resolution_table = context->first_name_resolution_table;
8886 69 bool res = arg->fix_fields(thd, &arg);
8887 69 context->last_name_resolution_table = orig_next_table;
8888
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 66 times.
69 if (res) return true;
8889 }
8890
8891 168 arg = arg->real_item();
8892
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 164 times.
168 if (arg->type() != FIELD_ITEM) {
8893 4 my_error(ER_BAD_FIELD_ERROR, MYF(0), "", "VALUES() function");
8894 4 return true;
8895 }
8896
8897 164 Item_field *field_arg = down_cast<Item_field *>(arg);
8898
8899
2/2
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 8 times.
164 if (thd->lex->in_update_value_clause &&
8900
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 3 times.
156 field_arg->field->table->insert_values) {
8901 153 Field *def_field = field_arg->field->clone(thd->mem_root);
8902
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 153 times.
153 if (def_field == nullptr) return true;
8903
8904 153 def_field->move_field_offset((ptrdiff_t)(def_field->table->insert_values -
8905 153 def_field->table->record[0]));
8906 153 m_rowbuffer_saved = def_field->table->insert_values;
8907 /*
8908 Put the original and cloned Field_blob objects in
8909 'insert_update_values_map' map. This will be used to make a
8910 separate copy of blob value, in case 'UPDATE' clause is executed in
8911 'INSERT...UPDATE' statement. See mysql_prepare_blob_values()
8912 for more info. We are only checking for MYSQL_TYPE_BLOB and
8913 MYSQL_TYPE_GEOMETRY. Sub types of blob like TINY BLOB, LONG BLOB, JSON,
8914 are internally stored are BLOB only. Same applies to geometry type.
8915 */
8916
4/4
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 143 times.
296 if ((def_field->type() == MYSQL_TYPE_BLOB ||
8917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143 times.
143 def_field->type() == MYSQL_TYPE_GEOMETRY)) {
8918 try {
8919
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 thd->lex->insert_values_map(field_arg, def_field);
8920 } catch (std::bad_alloc const &) {
8921 my_error(ER_STD_BAD_ALLOC_ERROR, MYF(0), "", "fix_fields");
8922 return true;
8923 }
8924 }
8925
8926 153 set_field(def_field);
8927
8928 // Use same field name as the underlying field:
8929
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 153 times.
153 assert(field_name == nullptr);
8930 153 field_name = arg->item_name.ptr();
8931
8932 // The VALUES function is deprecated.
8933
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 102 times.
153 if (m_is_values_function)
8934 51 push_deprecated_warn(
8935 thd, "VALUES function",
8936 "an alias (INSERT INTO ... VALUES (...) AS alias) and replace "
8937 "VALUES(col) in the ON DUPLICATE KEY UPDATE clause with alias.col");
8938 153 } else {
8939 // VALUES() is used out-of-scope - its value is always NULL
8940
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
11 Item *const item = new Item_null(this->item_name);
8941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (item == nullptr) return true;
8942 11 *reference = item;
8943
8944 // Ensure the object is not handled by bind_fields()
8945 11 arg = nullptr;
8946
8947 // The VALUES function is deprecated. It always returns NULL in this
8948 // context, but if it is inside an ON DUPLICATE KEY UPDATE clause, the user
8949 // probably meant something else. In that case, suggest an alternative
8950 // syntax which doesn't always return NULL.
8951
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 assert(m_is_values_function);
8952
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 8 times.
11 if (thd->lex->in_update_value_clause) {
8953 3 push_warning(thd, Sql_condition::SL_WARNING, ER_WARN_DEPRECATED_SYNTAX,
8954 ER_THD(thd, ER_WARN_DEPRECATED_VALUES_FUNCTION_ALWAYS_NULL));
8955 } else {
8956 8 push_deprecated_warn_no_replacement(thd, "VALUES function");
8957 }
8958 }
8959 164 return false;
8960 }
8961
8962 1014 void Item_insert_value::bind_fields() {
8963
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1014 times.
1014 if (arg == nullptr) return;
8964
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1014 times.
1014 if (!fixed) return;
8965
8966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1014 times.
1014 assert(table_ref->table->insert_values);
8967
8968 // Bind field to the current TABLE object
8969 1014 field->table = table_ref->table;
8970
8971 1014 field->move_field_offset(
8972 1014 (ptrdiff_t)(field->table->insert_values - m_rowbuffer_saved));
8973 1014 m_rowbuffer_saved = field->table->insert_values;
8974
8975 1014 Item_field *field_arg = down_cast<Item_field *>(arg->real_item());
8976
4/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 999 times.
✓ Branch 2 taken 999 times.
✓ Branch 3 taken 15 times.
1029 if ((field->type() == MYSQL_TYPE_BLOB ||
8977
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 field->type() == MYSQL_TYPE_GEOMETRY)) {
8978 999 current_thd->lex->insert_values_map(field_arg, field);
8979 }
8980
8981 1014 set_result_field(field);
8982 }
8983
8984 1197 void Item_insert_value::cleanup() {
8985 // Disconnect from the TABLE object
8986
2/2
✓ Branch 0 taken 1167 times.
✓ Branch 1 taken 30 times.
1197 if (field != nullptr) field->table = nullptr;
8987 1197 Item::cleanup();
8988 1197 }
8989
8990 void Item_insert_value::print(const THD *thd, String *str,
8991 enum_query_type query_type) const {
8992 str->append(STRING_WITH_LEN("values("));
8993 arg->print(thd, str, query_type);
8994 str->append(')');
8995 }
8996
8997 /**
8998 Find index of Field object which will be appropriate for item
8999 representing field of row being changed in trigger.
9000
9001 @param table_triggers Table_trigger_field_support instance. Do not use
9002 TABLE::triggers as it might be not initialized at
9003 the moment.
9004 @param table_grant_info GRANT_INFO of the subject table
9005
9006 @note
9007 This function does almost the same as fix_fields() for Item_field but is
9008 invoked right after trigger definition parsing. Since at this stage we can't
9009 say exactly what Field object (corresponding to TABLE::record[0] or
9010 TABLE::record[1]) should be bound to this Item, we only find out index of
9011 the Field and then select concrete Field object in fix_fields() (by that
9012 time Table_trigger_dispatcher::old_field/ new_field should point to proper
9013 array of Fields). It also binds Item_trigger_field to
9014 Table_trigger_field_support object for table of trigger which uses this
9015 item.
9016 Another difference is that the field is not marked in read_set/write_set.
9017 */
9018
9019 71312 void Item_trigger_field::setup_field(
9020 Table_trigger_field_support *table_triggers, GRANT_INFO *table_grant_info) {
9021 /*
9022 Try to find field by its name and if it will be found
9023 set field_idx properly.
9024 */
9025 71312 (void)find_field_in_table(table_triggers->get_subject_table(), field_name,
9026 false, &field_idx);
9027 71312 triggers = table_triggers;
9028 71312 table_grants = table_grant_info;
9029 71312 }
9030
9031 21 bool Item_trigger_field::eq(const Item *item, bool) const {
9032 21 return item->type() == TRIGGER_FIELD_ITEM &&
9033 17 trigger_var_type ==
9034
3/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
38 down_cast<const Item_trigger_field *>(item)->trigger_var_type &&
9035
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1 times.
38 !my_strcasecmp(
9036 system_charset_info, field_name,
9037 down_cast<const Item_trigger_field *>(item)->field_name);
9038 }
9039
9040 44042 bool Item_trigger_field::set_value(THD *thd, sp_rcontext * /*ctx*/, Item **it) {
9041 44042 Item *item = sp_prepare_func_item(thd, it);
9042
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 44036 times.
44042 if (item == nullptr) return true;
9043
9044
2/2
✓ Branch 0 taken 7564 times.
✓ Branch 1 taken 36472 times.
44036 if (!fixed) {
9045
1/2
✓ Branch 0 taken 7564 times.
✗ Branch 1 not taken.
7564 Prepared_stmt_arena_holder ps_arena_holder(thd);
9046
9047
3/4
✓ Branch 0 taken 7564 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 7538 times.
7564 if (fix_fields(thd, nullptr)) return true;
9048
2/2
✓ Branch 0 taken 7538 times.
✓ Branch 1 taken 26 times.
7564 } else {
9049
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 36464 times.
36472 if (walk(&Item::check_column_privileges, enum_walk::PREFIX,
9050 pointer_cast<uchar *>(thd)))
9051 8 return true;
9052 }
9053
9054 // NOTE: field->table->copy_blobs should be false here, but let's
9055 // remember the value at runtime to avoid subtle bugs.
9056 44002 bool copy_blobs_saved = field->table->copy_blobs;
9057
9058 44002 field->table->copy_blobs = true;
9059
9060 44002 int err_code = item->save_in_field(field, false);
9061
9062 44002 field->table->copy_blobs = copy_blobs_saved;
9063
9064 44002 return err_code < 0;
9065 }
9066
9067 38790 bool Item_trigger_field::fix_fields(THD *thd, Item **) {
9068 /*
9069 Since trigger is object tightly associated with TABLE object most
9070 of its set up can be performed during trigger loading i.e. trigger
9071 parsing! So we have little to do in fix_fields. :)
9072 */
9073
9074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38790 times.
38790 assert(fixed == 0);
9075
9076 /* Set field. */
9077
9078
2/2
✓ Branch 0 taken 38759 times.
✓ Branch 1 taken 31 times.
38790 if (field_idx != (uint)-1) {
9079 /*
9080 Check access privileges for the subject table. We check privileges only
9081 in runtime.
9082 */
9083
9084
2/2
✓ Branch 0 taken 20292 times.
✓ Branch 1 taken 18467 times.
38759 if (table_grants) {
9085
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 20244 times.
40584 if (check_grant_column(
9086 20292 thd, table_grants, triggers->get_subject_table()->s->db.str,
9087 20292 triggers->get_subject_table()->s->table_name.str, field_name,
9088 strlen(field_name), thd->security_context(), want_privilege))
9089 48 return true;
9090 }
9091
9092 38711 field = triggers->get_trigger_variable_field(trigger_var_type, field_idx);
9093
9094 38711 set_field(field);
9095 38711 return false;
9096 }
9097
9098 31 my_error(ER_BAD_FIELD_ERROR, MYF(0), field_name,
9099
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 13 times.
31 (trigger_var_type == TRG_NEW_ROW) ? "NEW" : "OLD");
9100 31 return true;
9101 }
9102
9103 115949 void Item_trigger_field::bind_fields() {
9104 // Triggers are tied to a TABLE, so fields will never relocate.
9105
9106
2/2
✓ Branch 0 taken 10599 times.
✓ Branch 1 taken 105350 times.
115949 if (!fixed) return;
9107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 105350 times.
105350 assert(field_idx != (uint)-1);
9108
9109 /*
9110 If the trigger's substatement using this object was previously invoked by a
9111 calling statement, and is now invoked by another, it may be that the two
9112 callers put the "old" record in a different place (for example, for a DELETE
9113 trigger, REPLACE uses TABLE::record[1] while DELETE uses TABLE::record[0],
9114 see the argument old_row_is_record1 in
9115 Table_trigger_dispatcher::process_triggers()). Thus 'field' needs an update
9116 for the second caller.
9117 */
9118
9119 105350 field = triggers->get_trigger_variable_field(trigger_var_type, field_idx);
9120
9121 105350 set_field(field);
9122 }
9123
9124 72824 bool Item_trigger_field::check_column_privileges(uchar *arg) {
9125 72824 THD *const thd = pointer_cast<THD *>(arg);
9126 72824 TABLE *table = triggers->get_subject_table();
9127
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 72815 times.
145648 if (check_grant_column(thd, table_grants, table->s->db.str,
9128 72824 table->s->table_name.str, field_name,
9129 strlen(field_name), thd->security_context(),
9130 want_privilege))
9131 9 return true;
9132 72815 return false;
9133 }
9134
9135 8 void Item_trigger_field::print(const THD *, String *str,
9136 enum_query_type) const {
9137
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 str->append((trigger_var_type == TRG_NEW_ROW) ? "NEW" : "OLD", 3);
9138 8 str->append('.');
9139
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 str->append(field_name);
9140 8 }
9141
9142 125699 void Item_trigger_field::cleanup() {
9143 /*
9144 A trigger is bound to a TABLE, so the TABLE_LIST may vary between executions
9145 */
9146 125699 table_ref = nullptr;
9147
9148 125699 Item::cleanup();
9149 125699 }
9150
9151 144676810 Item_result item_cmp_type(Item_result a, Item_result b) {
9152
2/2
✓ Branch 0 taken 144452560 times.
✓ Branch 1 taken 224250 times.
144676810 if (a == b) {
9153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 144452560 times.
144452560 assert(a != INVALID_RESULT);
9154 144452560 return a;
9155
3/4
✓ Branch 0 taken 224273 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 822 times.
✓ Branch 3 taken 223451 times.
224250 } else if (a == ROW_RESULT || b == ROW_RESULT) {
9156 799 return ROW_RESULT;
9157 }
9158
6/6
✓ Branch 0 taken 112714 times.
✓ Branch 1 taken 110737 times.
✓ Branch 2 taken 38175 times.
✓ Branch 3 taken 74539 times.
✓ Branch 4 taken 113388 times.
✓ Branch 5 taken 35524 times.
223451 if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
9159
2/2
✓ Branch 0 taken 30247 times.
✓ Branch 1 taken 83141 times.
113388 (b == INT_RESULT || b == DECIMAL_RESULT)) {
9160 65771 return DECIMAL_RESULT;
9161 }
9162 157680 return REAL_RESULT;
9163 }
9164
9165 /**
9166 Substitute a const item with a simpler const item, if possible.
9167
9168 @param thd Current session.
9169 @param[in,out] ref Const item to be processed, contains simplest possible
9170 item on return.
9171 @param comp_item Item that provides result type for generated const item
9172
9173 @returns false if success, true if error
9174 */
9175
9176 134012 bool resolve_const_item(THD *thd, Item **ref, Item *comp_item) {
9177 134012 Item *item = *ref;
9178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 134012 times.
134012 assert(item->const_item());
9179
9180 134012 Item *new_item = nullptr;
9181
2/2
✓ Branch 0 taken 133575 times.
✓ Branch 1 taken 437 times.
134012 if (item->basic_const_item()) return false; // Can't be better
9182 Item_result res_type =
9183 437 item_cmp_type(comp_item->result_type(), item->result_type());
9184
4/6
✓ Branch 0 taken 335 times.
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
437 switch (res_type) {
9185 335 case STRING_RESULT: {
9186
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 333 times.
335 if (item->data_type() == MYSQL_TYPE_JSON) {
9187 2 auto wr = make_unique_destroy_only<Json_wrapper>(thd->mem_root);
9188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (wr == nullptr) return true;
9189
3/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
2 if (item->val_json(wr.get())) return true;
9190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (item->null_value)
9191 new_item = new Item_null(item->item_name);
9192 else
9193
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 new_item = new Item_json(std::move(wr), item->item_name);
9194 1 break;
9195
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 }
9196 char buff[MAX_FIELD_WIDTH];
9197 333 String tmp(buff, sizeof(buff), &my_charset_bin), *result;
9198
1/2
✓ Branch 0 taken 333 times.
✗ Branch 1 not taken.
333 result = item->val_str(&tmp);
9199
2/4
✓ Branch 0 taken 333 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 333 times.
333 if (thd->is_error()) return true;
9200
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 332 times.
333 if (item->null_value)
9201
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 new_item = new Item_null(item->item_name);
9202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 332 times.
332 else if (item->is_temporal()) {
9203 enum_field_types type = item->data_type() == MYSQL_TYPE_TIMESTAMP
9204 ? MYSQL_TYPE_DATETIME
9205 : item->data_type();
9206 new_item = create_temporal_literal(thd, result->ptr(), result->length(),
9207 result->charset(), type, true);
9208 } else {
9209 332 size_t length = result->length();
9210
1/2
✓ Branch 0 taken 332 times.
✗ Branch 1 not taken.
332 char *tmp_str = sql_strmake(result->ptr(), length);
9211
1/2
✓ Branch 0 taken 332 times.
✗ Branch 1 not taken.
664 new_item = new Item_string(item->item_name, tmp_str, length,
9212
1/2
✓ Branch 0 taken 332 times.
✗ Branch 1 not taken.
664 result->charset());
9213 }
9214 333 break;
9215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 333 times.
333 }
9216 39 case INT_RESULT: {
9217 39 longlong result = item->val_int();
9218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (thd->is_error()) return true;
9219 39 uint length = item->max_length;
9220 39 bool null_value = item->null_value;
9221
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 31 times.
39 if (null_value)
9222
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 new_item = new Item_null(item->item_name);
9223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 else if (item->unsigned_flag)
9224 new_item = new Item_uint(item->item_name, result, length);
9225 else
9226
2/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
31 new_item = new Item_int(item->item_name, result, length);
9227 39 break;
9228 }
9229 60 case ROW_RESULT: {
9230 /*
9231 Substitute constants only in Item_rows. Don't affect other Items
9232 with ROW_RESULT (eg Item_singlerow_subselect).
9233
9234 For such Items more optimal is to detect if it is constant and replace
9235 it with Item_row. This would optimize queries like this:
9236 SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
9237 */
9238
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
120 if (!(item->type() == Item::ROW_ITEM &&
9239
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 comp_item->type() == Item::ROW_ITEM))
9240 60 return false;
9241 Item_row *item_row = (Item_row *)item;
9242 Item_row *comp_item_row = (Item_row *)comp_item;
9243 /*
9244 If item and comp_item are both Item_rows and have same number of cols
9245 then process items in Item_row one by one.
9246 We can't ignore NULL values here as this item may be used with <=>, in
9247 which case NULL's are significant.
9248 */
9249 assert(item->result_type() == comp_item->result_type());
9250 assert(item_row->cols() == comp_item_row->cols());
9251 uint col = item_row->cols();
9252 while (col-- > 0)
9253 if (resolve_const_item(thd, item_row->addr(col),
9254 comp_item_row->element_index(col)))
9255 return true;
9256 break;
9257 }
9258 3 case REAL_RESULT: { // It must REAL_RESULT
9259 3 double result = item->val_real();
9260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (thd->is_error()) return true;
9261 3 uint length = item->max_length, decimals = item->decimals;
9262 3 bool null_value = item->null_value;
9263
3/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
3 new_item = (null_value ? (Item *)new Item_null(item->item_name)
9264 : (Item *)new Item_float(item->item_name, result,
9265 decimals, length));
9266 3 break;
9267 }
9268 case DECIMAL_RESULT: {
9269 my_decimal decimal_value;
9270 my_decimal *result = item->val_decimal(&decimal_value);
9271 if (thd->is_error()) return true;
9272 bool null_value = item->null_value;
9273 new_item = (null_value ? (Item *)new Item_null(item->item_name)
9274 : (Item *)new Item_decimal(item->item_name, result,
9275 item->decimals,
9276 item->max_length));
9277 break;
9278 }
9279 default:
9280 assert(0);
9281 }
9282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 376 times.
376 if (new_item == nullptr) return true;
9283
9284 376 *ref = new_item;
9285
9286 376 return false;
9287 }
9288
9289 /**
9290 Compare the value stored in field with the expression from the query.
9291
9292 @param thd Current session.
9293 @param field Field which the Item is stored in after conversion
9294 @param item Original expression from query
9295
9296 @return Returns an integer greater than, equal to, or less than 0 if
9297 the value stored in the field is greater than, equal to,
9298 or less than the original Item. A 0 may also be returned if
9299 out of memory.
9300
9301 @note We use this in the range optimizer/partition pruning,
9302 because in some cases we can't store the value in the field
9303 without some precision/character loss.
9304
9305 We similarly use it to verify that expressions like
9306 BIGINT_FIELD @<cmp@> @<literal value@>
9307 is done correctly (as int/decimal/float according to literal type).
9308 */
9309
9310 10347203 int stored_field_cmp_to_item(THD *thd, Field *field, Item *item) {
9311 Item_result res_type =
9312 10347203 item_cmp_type(field->result_type(), item->result_type());
9313
6/6
✓ Branch 0 taken 395 times.
✓ Branch 1 taken 10346811 times.
✓ Branch 2 taken 387 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 387 times.
✓ Branch 5 taken 10346819 times.
10347600 if (field->type() == MYSQL_TYPE_TIME &&
9314 395 item->data_type() == MYSQL_TYPE_TIME) {
9315 387 longlong field_value = field->val_time_temporal();
9316 387 longlong item_value = item->val_time_temporal();
9317
2/4
✓ Branch 0 taken 387 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 387 times.
387 return field_value < item_value ? -1 : field_value > item_value ? 1 : 0;
9318 }
9319
6/6
✓ Branch 0 taken 3969 times.
✓ Branch 1 taken 10342848 times.
✓ Branch 2 taken 3086 times.
✓ Branch 3 taken 883 times.
✓ Branch 4 taken 3086 times.
✓ Branch 5 taken 10343731 times.
10346819 if (is_temporal_type_with_date(field->type()) && item->is_temporal()) {
9320 /*
9321 Note, in case of TIME data type we also go here
9322 and call item->val_date_temporal(), because we want
9323 TIME to be converted to DATE/DATETIME properly.
9324 Only non-temporal data types go though get_mysql_time_from_str()
9325 in the below code branch.
9326 */
9327 3086 longlong field_value = field->val_date_temporal();
9328 3086 longlong item_value = item->val_date_temporal();
9329
4/4
✓ Branch 0 taken 3037 times.
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3035 times.
3086 return field_value < item_value ? -1 : field_value > item_value ? 1 : 0;
9330 }
9331
2/2
✓ Branch 0 taken 10273443 times.
✓ Branch 1 taken 70288 times.
10343731 if (res_type == STRING_RESULT) {
9332 char item_buff[MAX_FIELD_WIDTH];
9333 char field_buff[MAX_FIELD_WIDTH];
9334
9335 10273443 String item_tmp(item_buff, sizeof(item_buff), &my_charset_bin);
9336 10273443 String field_tmp(field_buff, sizeof(field_buff), &my_charset_bin);
9337
1/2
✓ Branch 0 taken 10273443 times.
✗ Branch 1 not taken.
10273443 String *item_result = item->val_str(&item_tmp);
9338 /*
9339 Some implementations of Item::val_str(String*) actually modify
9340 the field Item::null_value, hence we can't check it earlier.
9341 */
9342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10273443 times.
10273443 if (item->null_value) return 0;
9343
1/2
✓ Branch 0 taken 10273443 times.
✗ Branch 1 not taken.
10273443 String *field_result = field->val_str(&field_tmp);
9344
9345
3/4
✓ Branch 0 taken 10273443 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 883 times.
✓ Branch 3 taken 10272560 times.
10273443 if (is_temporal_type_with_date(field->type())) {
9346 enum_mysql_timestamp_type type =
9347
1/2
✓ Branch 0 taken 883 times.
✗ Branch 1 not taken.
883 field_type_to_timestamp_type(field->type());
9348 883 const char *field_name = field->field_name;
9349 MYSQL_TIME field_time, item_time;
9350
1/2
✓ Branch 0 taken 883 times.
✗ Branch 1 not taken.
883 get_mysql_time_from_str(thd, field_result, type, field_name, &field_time);
9351
1/2
✓ Branch 0 taken 883 times.
✗ Branch 1 not taken.
883 get_mysql_time_from_str(thd, item_result, type, field_name, &item_time);
9352 /*
9353 If the string represents a UTC timestamp (with timezone
9354 offset), convert it to a datetime in the current time zone.
9355 */
9356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 883 times.
883 if (item_time.time_type == MYSQL_TIMESTAMP_DATETIME_TZ)
9357 convert_time_zone_displacement(current_thd->time_zone(), &item_time);
9358
9359
2/4
✓ Branch 0 taken 883 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 883 times.
✗ Branch 3 not taken.
883 assert(field_time.time_type != MYSQL_TIMESTAMP_DATETIME_TZ &&
9360 item_time.time_type != MYSQL_TIMESTAMP_DATETIME_TZ);
9361
1/2
✓ Branch 0 taken 883 times.
✗ Branch 1 not taken.
883 return my_time_compare(field_time, item_time);
9362 }
9363
2/4
✓ Branch 0 taken 10272560 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10272560 times.
✗ Branch 3 not taken.
10272560 return sortcmp(field_result, item_result, field->charset());
9364 10273443 }
9365
2/2
✓ Branch 0 taken 66038 times.
✓ Branch 1 taken 4250 times.
70288 if (res_type == INT_RESULT) return 0; // Both are of type int
9366
2/2
✓ Branch 0 taken 1632 times.
✓ Branch 1 taken 2618 times.
4250 if (res_type == DECIMAL_RESULT) {
9367 1632 my_decimal item_buf, *item_val, field_buf, *field_val;
9368
1/2
✓ Branch 0 taken 1632 times.
✗ Branch 1 not taken.
1632 item_val = item->val_decimal(&item_buf);
9369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1632 times.
1632 if (item->null_value) return 0;
9370
1/2
✓ Branch 0 taken 1632 times.
✗ Branch 1 not taken.
1632 field_val = field->val_decimal(&field_buf);
9371
1/2
✓ Branch 0 taken 1632 times.
✗ Branch 1 not taken.
1632 return my_decimal_cmp(field_val, item_val);
9372 1632 }
9373 /*
9374 The patch for Bug#13463415 started using this function for comparing
9375 BIGINTs. That uncovered a bug in Visual Studio 32bit optimized mode.
9376 Prefixing the auto variables with volatile fixes the problem....
9377 */
9378 2618 volatile double result = item->val_real();
9379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2617 times.
2617 if (item->null_value) return 0;
9380 2617 volatile double field_result = field->val_real();
9381
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 2553 times.
2617 if (field_result < result)
9382 64 return -1;
9383
2/2
✓ Branch 0 taken 1198 times.
✓ Branch 1 taken 1355 times.
2553 else if (field_result > result)
9384 1198 return 1;
9385 1355 return 0;
9386 }
9387
9388 333287 Item_cache *Item_cache::get_cache(const Item *item) {
9389 333287 return get_cache(item, item->result_type());
9390 }
9391
9392 /**
9393 Get a cache item of given type.
9394
9395 @param item value to be cached
9396 @param type required type of cache
9397
9398 @return cache item
9399 */
9400
9401 389879 Item_cache *Item_cache::get_cache(const Item *item, const Item_result type) {
9402
5/6
✓ Branch 0 taken 269169 times.
✓ Branch 1 taken 55714 times.
✓ Branch 2 taken 13015 times.
✓ Branch 3 taken 49304 times.
✓ Branch 4 taken 2677 times.
✗ Branch 5 not taken.
389879 switch (type) {
9403 269169 case INT_RESULT:
9404 /*
9405 When it's an item of MYSQL_TYPE_BIT, we need to retain its result
9406 as bit format instead of an integer.
9407 */
9408
2/2
✓ Branch 0 taken 313 times.
✓ Branch 1 taken 268856 times.
269169 if (item->data_type() == MYSQL_TYPE_BIT)
9409
2/4
✓ Branch 0 taken 313 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 313 times.
✗ Branch 3 not taken.
313 return new Item_cache_bit(item->data_type());
9410
2/4
✓ Branch 0 taken 268856 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 268856 times.
✗ Branch 3 not taken.
268856 return new Item_cache_int(item->data_type());
9411 55714 case REAL_RESULT:
9412
1/2
✓ Branch 0 taken 55714 times.
✗ Branch 1 not taken.
111428 return new Item_cache_real();
9413 13015 case DECIMAL_RESULT:
9414
1/2
✓ Branch 0 taken 13015 times.
✗ Branch 1 not taken.
26030 return new Item_cache_decimal();
9415 49304 case STRING_RESULT:
9416 /* Not all functions that return DATE/TIME are actually DATE/TIME funcs.
9417 */
9418
2/2
✓ Branch 0 taken 2154 times.
✓ Branch 1 taken 47150 times.
49304 if (item->is_temporal())
9419
2/4
✓ Branch 0 taken 2154 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2154 times.
✗ Branch 3 not taken.
2154 return new Item_cache_datetime(item->data_type());
9420
3/4
✓ Branch 0 taken 1860 times.
✓ Branch 1 taken 45290 times.
✓ Branch 2 taken 1860 times.
✗ Branch 3 not taken.
49010 if (item->data_type() == MYSQL_TYPE_JSON) return new Item_cache_json();
9421
2/4
✓ Branch 0 taken 45290 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 45290 times.
✗ Branch 3 not taken.
45290 return new Item_cache_str(item);
9422 2677 case ROW_RESULT:
9423
1/2
✓ Branch 0 taken 2677 times.
✗ Branch 1 not taken.
5354 return new Item_cache_row();
9424 default:
9425 // should never be in real life
9426 assert(0);
9427 return nullptr;
9428 }
9429 }
9430
9431 1154050 void Item_cache::store(Item *item) {
9432
2/2
✓ Branch 0 taken 985339 times.
✓ Branch 1 taken 168714 times.
1154050 if (current_thd->lex->is_exec_started())
9433 985339 current_thd->change_item_tree(&example, item);
9434 else
9435 168714 example = item;
9436
2/2
✓ Branch 0 taken 46896 times.
✓ Branch 1 taken 1107155 times.
1154051 if (!item) {
9437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46896 times.
46896 assert(is_nullable());
9438 46896 null_value = true;
9439 }
9440 1154051 value_cached = false;
9441 1154051 }
9442
9443 5362 void Item_cache::print(const THD *thd, String *str,
9444 enum_query_type query_type) const {
9445 5362 str->append(STRING_WITH_LEN("<cache>("));
9446
1/2
✓ Branch 0 taken 5362 times.
✗ Branch 1 not taken.
5362 if (example)
9447 5362 example->print(thd, str, query_type);
9448 else
9449 Item::print(thd, str, query_type);
9450 5362 str->append(')');
9451 5362 }
9452
9453 51048 bool Item_cache::walk(Item_processor processor, enum_walk walk, uchar *arg) {
9454
3/4
✓ Branch 0 taken 3102 times.
✓ Branch 1 taken 7051 times.
✓ Branch 2 taken 10153 times.
✗ Branch 3 not taken.
61201 return ((walk & enum_walk::PREFIX) && (this->*processor)(arg)) ||
9455
7/8
✓ Branch 0 taken 10153 times.
✓ Branch 1 taken 40895 times.
✓ Branch 2 taken 51048 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 50921 times.
✓ Branch 5 taken 127 times.
✓ Branch 6 taken 42554 times.
✓ Branch 7 taken 8367 times.
154676 (example && example->walk(processor, walk, arg)) ||
9456
4/4
✓ Branch 0 taken 42520 times.
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 456 times.
✓ Branch 3 taken 42098 times.
144523 ((walk & enum_walk::POSTFIX) && (this->*processor)(arg));
9457 }
9458
9459 153138765 bool Item_cache::has_value() {
9460
6/6
✓ Branch 0 taken 138624 times.
✓ Branch 1 taken 153000141 times.
✓ Branch 2 taken 138622 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 153138762 times.
✓ Branch 5 taken 3 times.
153138765 if (value_cached || cache_value()) {
9461 /*
9462 Only expect NULL if the cache is nullable, or if an error was
9463 raised when reading the value into the cache.
9464 */
9465
5/6
✓ Branch 0 taken 5395 times.
✓ Branch 1 taken 153133367 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 5383 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 10 times.
153138762 assert(!null_value || is_nullable() || current_thd->is_error());
9466 153138760 return !null_value;
9467 }
9468 3 return false;
9469 }
9470
9471 1415287 void Item_cache::cleanup() {
9472 /*
9473 In case the cache wraps a dynamic parameter, user variable (=> there is an
9474 'example' item), any next execution should cache the new value.
9475 If no 'example', caching is done through store_value() and that's for
9476 objects which are constant over all executions.
9477 */
9478
2/2
✓ Branch 0 taken 1373764 times.
✓ Branch 1 taken 41523 times.
1415287 if (example != nullptr) clear();
9479 1415290 Item::cleanup();
9480 1415292 }
9481
9482 74769152 bool Item_cache_int::cache_value() {
9483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74769152 times.
74769152 if (!example) return false;
9484 74769152 value_cached = true;
9485 74769152 value = example->val_int();
9486 74769156 null_value = example->null_value;
9487 74769156 unsigned_flag = example->unsigned_flag;
9488 74769156 return true;
9489 }
9490
9491 void Item_cache_int::store_value(Item *item, longlong val_arg) {
9492 /* An explicit values is given, save it. */
9493 value_cached = true;
9494 value = val_arg;
9495 null_value = item->null_value;
9496 unsigned_flag = item->unsigned_flag;
9497 }
9498
9499 166 String *Item_cache_int::val_str(String *str) {
9500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 166 times.
166 assert(fixed == 1);
9501
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 164 times.
166 if (!has_value()) return nullptr;
9502 164 str->set_int(value, unsigned_flag, default_charset());
9503 164 return str;
9504 }
9505
9506 2297 my_decimal *Item_cache_int::val_decimal(my_decimal *decimal_val) {
9507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2297 times.
2297 assert(fixed == 1);
9508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2297 times.
2297 if (!has_value()) return nullptr;
9509 2297 int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_val);
9510 2297 return decimal_val;
9511 }
9512
9513 2104 double Item_cache_int::val_real() {
9514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2104 times.
2104 assert(fixed == 1);
9515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2104 times.
2104 if (!has_value()) return 0.0;
9516
2/2
✓ Branch 0 taken 478 times.
✓ Branch 1 taken 1626 times.
2104 if (unsigned_flag) return static_cast<unsigned long long>(value);
9517 1626 return value;
9518 }
9519
9520 151290426 longlong Item_cache_int::val_int() {
9521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 151290426 times.
151290426 assert(fixed == 1);
9522
2/2
✓ Branch 0 taken 171 times.
✓ Branch 1 taken 151290256 times.
151290426 if (!has_value()) return 0;
9523 151290256 return value;
9524 }
9525
9526 6 String *Item_cache_bit::val_str(String *str) {
9527
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 assert(fixed);
9528
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (!has_value()) return nullptr;
9529
9530 char buff[sizeof(longlong)];
9531 6 mi_int8store(buff, value);
9532 6 uint offset = sizeof(longlong) - string_length();
9533
9534 // for BIT(N), copy last N bits from buff
9535 // (rounded up to an integral number of bytes)
9536 6 str->length(0);
9537
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (str->append(buff + offset, string_length())) return nullptr;
9538
9539 6 return str;
9540 }
9541
9542 6864 bool Item_cache_datetime::cache_value_int() {
9543
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6864 times.
6864 if (!example) return false;
9544
9545 6864 value_cached = true;
9546 // Mark cached string value obsolete
9547 6864 str_value_cached = false;
9548
9549
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6864 times.
6864 assert(data_type() == example->data_type());
9550 6864 int_value = example->val_temporal_by_field_type();
9551 6864 null_value = example->null_value;
9552 6864 unsigned_flag = example->unsigned_flag;
9553
9554 6864 return true;
9555 }
9556
9557 10605 bool Item_cache_datetime::cache_value() {
9558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10605 times.
10605 if (!example) return false;
9559
9560
2/2
✓ Branch 0 taken 5804 times.
✓ Branch 1 taken 4801 times.
10605 if (cmp_context == INT_RESULT) return cache_value_int();
9561
9562 4801 str_value_cached = true;
9563 // Mark cached int value obsolete
9564 4801 value_cached = false;
9565 /* Assume here that the underlying item will do correct conversion.*/
9566 4801 String *res = example->val_str(&cached_string);
9567
4/4
✓ Branch 0 taken 4162 times.
✓ Branch 1 taken 639 times.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 4139 times.
4801 if (res && res != &cached_string) cached_string.copy(*res);
9568 4801 null_value = example->null_value;
9569 4801 unsigned_flag = example->unsigned_flag;
9570 4801 return true;
9571 }
9572
9573 6334 void Item_cache_datetime::store_value(Item *item, longlong val_arg) {
9574 /* An explicit values is given, save it. */
9575 6334 value_cached = true;
9576 6334 int_value = val_arg;
9577 6334 null_value = item->null_value;
9578 6334 unsigned_flag = item->unsigned_flag;
9579 6334 }
9580
9581 9162 void Item_cache_datetime::store(Item *item) {
9582 9162 Item_cache::store(item);
9583 9162 str_value_cached = false;
9584 9162 }
9585
9586 7025 String *Item_cache_datetime::val_str(String *) {
9587
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7025 times.
7025 assert(fixed == 1);
9588
9589
4/6
✓ Branch 0 taken 5462 times.
✓ Branch 1 taken 1563 times.
✓ Branch 2 taken 5462 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7025 times.
7025 if ((value_cached || str_value_cached) && null_value) return nullptr;
9590
9591
2/2
✓ Branch 0 taken 1181 times.
✓ Branch 1 taken 5844 times.
7025 if (!str_value_cached) {
9592 /*
9593 When it's possible the Item_cache_datetime uses INT datetime
9594 representation due to speed reasons. But still, it always has the STRING
9595 result type and thus it can be asked to return a string value.
9596 It is possible that at this time cached item doesn't contain correct
9597 string value, thus we have to convert cached int value to string and
9598 return it.
9599 */
9600
1/2
✓ Branch 0 taken 1181 times.
✗ Branch 1 not taken.
1181 if (value_cached) {
9601 MYSQL_TIME ltime;
9602
1/2
✓ Branch 0 taken 1181 times.
✗ Branch 1 not taken.
1181 TIME_from_longlong_packed(&ltime, data_type(), int_value);
9603 1181 if ((null_value =
9604 2362 my_TIME_to_str(&ltime, &cached_string,
9605
2/4
✓ Branch 0 taken 1181 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1181 times.
1181 min(decimals, uint8{DATETIME_MAX_DECIMALS}))))
9606 return nullptr;
9607 1181 str_value_cached = true;
9608 } else if (!cache_value() || null_value)
9609 return nullptr;
9610 }
9611 7025 return &cached_string;
9612 }
9613
9614 95 my_decimal *Item_cache_datetime::val_decimal(my_decimal *decimal_val) {
9615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 assert(fixed == 1);
9616
9617
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 73 times.
95 if (str_value_cached) {
9618
2/3
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
22 switch (data_type()) {
9619 13 case MYSQL_TYPE_TIME:
9620 13 return val_decimal_from_time(decimal_val);
9621 9 case MYSQL_TYPE_DATETIME:
9622 case MYSQL_TYPE_TIMESTAMP:
9623 case MYSQL_TYPE_DATE:
9624 9 return val_decimal_from_date(decimal_val);
9625 default:
9626 assert(0);
9627 return nullptr;
9628 }
9629 }
9630
9631
5/8
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 73 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 73 times.
73 if ((!value_cached && !cache_value_int()) || null_value) return nullptr;
9632 73 return my_decimal_from_datetime_packed(decimal_val, data_type(), int_value);
9633 }
9634
9635 2298 bool Item_cache_datetime::get_date(MYSQL_TIME *ltime,
9636 my_time_flags_t fuzzydate) {
9637
5/6
✓ Branch 0 taken 1999 times.
✓ Branch 1 taken 299 times.
✓ Branch 2 taken 1995 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2294 times.
2298 if ((value_cached || str_value_cached) && null_value) return true;
9638
9639
2/2
✓ Branch 0 taken 1995 times.
✓ Branch 1 taken 303 times.
2298 if (str_value_cached) // TS-TODO: reuse MYSQL_TIME_cache eventually.
9640 1995 return get_date_from_string(ltime, fuzzydate);
9641
9642
5/8
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 299 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 303 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 303 times.
303 if ((!value_cached && !cache_value_int()) || null_value)
9643 return (null_value = true);
9644
9645
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 81 times.
✓ Branch 2 taken 221 times.
✗ Branch 3 not taken.
303 switch (data_type()) {
9646 1 case MYSQL_TYPE_TIME: {
9647 MYSQL_TIME tm;
9648
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 TIME_from_longlong_time_packed(&tm, int_value);
9649
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 time_to_datetime(current_thd, &tm, ltime);
9650 1 return false;
9651 }
9652 81 case MYSQL_TYPE_DATE: {
9653 81 int warnings = 0;
9654
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 TIME_from_longlong_date_packed(ltime, int_value);
9655
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 return check_date(*ltime, non_zero_date(*ltime), fuzzydate, &warnings);
9656 }
9657 221 case MYSQL_TYPE_DATETIME:
9658 case MYSQL_TYPE_TIMESTAMP: {
9659 221 int warnings = 0;
9660
1/2
✓ Branch 0 taken 221 times.
✗ Branch 1 not taken.
221 TIME_from_longlong_datetime_packed(ltime, int_value);
9661
1/2
✓ Branch 0 taken 221 times.
✗ Branch 1 not taken.
221 return check_date(*ltime, non_zero_date(*ltime), fuzzydate, &warnings);
9662 }
9663 default:
9664 assert(0);
9665 }
9666 return true;
9667 }
9668
9669 959 bool Item_cache_datetime::get_time(MYSQL_TIME *ltime) {
9670
4/6
✓ Branch 0 taken 869 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 869 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 959 times.
959 if ((value_cached || str_value_cached) && null_value) return true;
9671
9672
2/2
✓ Branch 0 taken 869 times.
✓ Branch 1 taken 90 times.
959 if (str_value_cached) // TS-TODO: reuse MYSQL_TIME_cache eventually.
9673 869 return get_time_from_string(ltime);
9674
9675
3/8
✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 90 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 90 times.
90 if ((!value_cached && !cache_value_int()) || null_value) return true;
9676
9677
2/4
✓ Branch 0 taken 89 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
90 switch (data_type()) {
9678 89 case MYSQL_TYPE_TIME:
9679 89 TIME_from_longlong_time_packed(ltime, int_value);
9680 89 return false;
9681 1 case MYSQL_TYPE_DATE:
9682 1 set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
9683 1 return false;
9684 case MYSQL_TYPE_DATETIME:
9685 case MYSQL_TYPE_TIMESTAMP:
9686 TIME_from_longlong_datetime_packed(ltime, int_value);
9687 datetime_to_time(ltime);
9688 return false;
9689 default:
9690 assert(0);
9691 }
9692 return true;
9693 }
9694
9695 31 double Item_cache_datetime::val_real() { return val_real_from_decimal(); }
9696
9697 1167 longlong Item_cache_datetime::val_time_temporal() {
9698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1167 times.
1167 assert(fixed == 1);
9699
7/8
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1151 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 1159 times.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 1159 times.
1167 if ((!value_cached && !cache_value_int()) || null_value) return 0;
9700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1159 times.
1159 if (is_temporal_with_date()) {
9701 /* Convert packed date to packed time */
9702 MYSQL_TIME ltime;
9703 return get_time_from_date(&ltime)
9704 ? 0
9705 : TIME_to_longlong_packed(ltime, data_type());
9706 }
9707 1159 return int_value;
9708 }
9709
9710 11506 longlong Item_cache_datetime::val_date_temporal() {
9711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11506 times.
11506 assert(fixed == 1);
9712
7/8
✓ Branch 0 taken 1039 times.
✓ Branch 1 taken 10467 times.
✓ Branch 2 taken 1039 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 11495 times.
✓ Branch 6 taken 11 times.
✓ Branch 7 taken 11495 times.
11506 if ((!value_cached && !cache_value_int()) || null_value) return 0;
9713
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 11477 times.
11495 if (data_type() == MYSQL_TYPE_TIME) {
9714 /* Convert packed time to packed date */
9715 MYSQL_TIME ltime;
9716
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 return get_date_from_time(&ltime) ? 0
9717
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 : TIME_to_longlong_datetime_packed(ltime);
9718 }
9719 11477 return int_value;
9720 }
9721
9722 22 longlong Item_cache_datetime::val_int() { return val_int_from_decimal(); }
9723
9724 1910 Item_cache_json::Item_cache_json()
9725 : Item_cache(MYSQL_TYPE_JSON),
9726
1/2
✓ Branch 0 taken 1910 times.
✗ Branch 1 not taken.
3820 m_value(new (*THR_MALLOC) Json_wrapper()),
9727
1/2
✓ Branch 0 taken 1910 times.
✗ Branch 1 not taken.
3820 m_is_sorted(false) {}
9728
9729 7640 Item_cache_json::~Item_cache_json() { destroy(m_value); }
9730
9731 /**
9732 Read the JSON value and cache it.
9733 @return true if the value was successfully cached, false otherwise
9734 */
9735 2165 bool Item_cache_json::cache_value() {
9736
2/4
✓ Branch 0 taken 2165 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2165 times.
2165 if (!example || !m_value) return false;
9737
9738
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2151 times.
2165 if (json_value(example, m_value, &value_cached)) { // Error
9739 14 null_value = true; // Set the NULL indicator to prevent reading the value
9740 14 return false;
9741 }
9742 2151 null_value = example->null_value;
9743
9744
3/4
✓ Branch 0 taken 2151 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1911 times.
✓ Branch 3 taken 240 times.
2151 if (value_cached && !null_value) {
9745 // the row buffer might change, so need own copy
9746 1911 m_value->to_dom();
9747 }
9748 2151 m_is_sorted = false;
9749 2151 return value_cached;
9750 }
9751
9752 32 void Item_cache_json::store_value(Item *expr, Json_wrapper *wr) {
9753 32 value_cached = true;
9754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if ((null_value = expr->null_value))
9755 m_value = nullptr;
9756 else {
9757 32 *m_value = *wr;
9758 // the row buffer might change, so need own copy
9759 32 m_value->to_dom();
9760 }
9761 32 m_is_sorted = false;
9762 32 }
9763
9764 /**
9765 Copy the cached JSON value into a wrapper.
9766 @param[out] wr the wrapper that receives the JSON value
9767 */
9768 99958 bool Item_cache_json::val_json(Json_wrapper *wr) {
9769
5/6
✓ Branch 0 taken 99950 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 99950 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 99950 times.
✓ Branch 5 taken 8 times.
99958 if (has_value() && !null_value) *wr = *m_value;
9770 99958 return current_thd->is_error();
9771 }
9772
9773 /// Get the name of the cached field of an Item_cache_json instance.
9774 1309 inline static const char *whence(const Item_field *cached_field) {
9775
2/2
✓ Branch 0 taken 1260 times.
✓ Branch 1 taken 49 times.
1309 return cached_field != nullptr ? cached_field->field_name : "?";
9776 }
9777
9778 840 String *Item_cache_json::val_str(String *tmp) {
9779
2/2
✓ Branch 0 taken 839 times.
✓ Branch 1 taken 1 times.
840 if (has_value()) {
9780 839 tmp->length(0);
9781
1/2
✓ Branch 0 taken 839 times.
✗ Branch 1 not taken.
839 m_value->to_string(tmp, true, whence(cached_field),
9782 JsonDocumentDefaultDepthHandler);
9783 839 return tmp;
9784 }
9785
9786 1 return nullptr;
9787 }
9788
9789 48 double Item_cache_json::val_real() {
9790 48 Json_wrapper wr;
9791
9792
2/4
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
48 if (val_json(&wr)) return 0.0;
9793
9794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (null_value) return 0.0;
9795
9796
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 return wr.coerce_real(whence(cached_field));
9797 48 }
9798
9799 38 my_decimal *Item_cache_json::val_decimal(my_decimal *decimal_value) {
9800 38 Json_wrapper wr;
9801
9802
3/4
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 37 times.
38 if (val_json(&wr)) return decimal_value;
9803
9804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (null_value) return decimal_value;
9805
9806
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 return wr.coerce_decimal(decimal_value, whence(cached_field));
9807 38 }
9808
9809 107 bool Item_cache_json::get_date(MYSQL_TIME *ltime, my_time_flags_t) {
9810 107 Json_wrapper wr;
9811
9812
2/4
✓ Branch 0 taken 107 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 107 times.
107 if (val_json(&wr)) return true;
9813
9814
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 107 times.
107 if (null_value) return true;
9815
9816
1/2
✓ Branch 0 taken 107 times.
✗ Branch 1 not taken.
107 return wr.coerce_date(ltime, whence(cached_field));
9817 107 }
9818
9819 37 bool Item_cache_json::get_time(MYSQL_TIME *ltime) {
9820 37 Json_wrapper wr;
9821
9822
2/4
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37 times.
37 if (val_json(&wr)) return true;
9823
9824
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (null_value) return true;
9825
9826
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 return wr.coerce_time(ltime, whence(cached_field));
9827 37 }
9828
9829 241 longlong Item_cache_json::val_int() {
9830 241 Json_wrapper wr;
9831
2/4
✓ Branch 0 taken 241 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 241 times.
241 if (val_json(&wr)) return 0;
9832
9833
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 241 times.
241 if (null_value) return true;
9834
9835
1/2
✓ Branch 0 taken 241 times.
✗ Branch 1 not taken.
241 return wr.coerce_int(whence(cached_field));
9836 241 }
9837
9838 2 void Item_cache_json::sort() {
9839
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 assert(!m_is_sorted);
9840
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 if (has_value() && m_value->type() == enum_json_type::J_ARRAY) {
9841 2 m_value->sort();
9842 2 m_is_sorted = true;
9843 }
9844 2 }
9845
9846 64830 bool Item_cache_real::cache_value() {
9847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64830 times.
64830 if (!example) return false;
9848 64830 value_cached = true;
9849 64830 value = example->val_real();
9850 64830 null_value = example->null_value;
9851 64830 return true;
9852 }
9853
9854 void Item_cache_real::store_value(Item *expr, double d) {
9855 value_cached = true;
9856 value = d;
9857 null_value = expr->null_value;
9858 }
9859
9860 220548 double Item_cache_real::val_real() {
9861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 220548 times.
220548 assert(fixed == 1);
9862
2/2
✓ Branch 0 taken 5033 times.
✓ Branch 1 taken 215516 times.
220548 if (!has_value()) return 0.0;
9863 215516 return value;
9864 }
9865
9866 longlong Item_cache_real::val_int() {
9867 assert(fixed == 1);
9868 if (!has_value()) return 0;
9869 return (longlong)rint(value);
9870 }
9871
9872 29 String *Item_cache_real::val_str(String *str) {
9873
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 assert(fixed == 1);
9874
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!has_value()) return nullptr;
9875 29 str->set_real(value, decimals, default_charset());
9876 29 return str;
9877 }
9878
9879 1 my_decimal *Item_cache_real::val_decimal(my_decimal *decimal_val) {
9880
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(fixed == 1);
9881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!has_value()) return nullptr;
9882 1 double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
9883 1 return decimal_val;
9884 }
9885
9886 7342 bool Item_cache_decimal::cache_value() {
9887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7342 times.
7342 if (!example) return false;
9888 7342 value_cached = true;
9889 7342 my_decimal *val = example->val_decimal(&decimal_value);
9890
6/6
✓ Branch 0 taken 7104 times.
✓ Branch 1 taken 238 times.
✓ Branch 2 taken 1446 times.
✓ Branch 3 taken 5658 times.
✓ Branch 4 taken 1446 times.
✓ Branch 5 taken 5896 times.
7342 if (!(null_value = example->null_value) && val != &decimal_value)
9891 1446 my_decimal2decimal(val, &decimal_value);
9892 7342 return true;
9893 }
9894
9895 void Item_cache_decimal::store_value(Item *expr, my_decimal *d) {
9896 value_cached = true;
9897 null_value = expr->null_value;
9898 my_decimal cpy(*d);
9899 decimal_value.swap(cpy);
9900 }
9901
9902 382 double Item_cache_decimal::val_real() {
9903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 382 times.
382 assert(fixed);
9904 double res;
9905
2/4
✓ Branch 0 taken 382 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 382 times.
382 if (!has_value()) return 0.0;
9906
1/2
✓ Branch 0 taken 382 times.
✗ Branch 1 not taken.
382 my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &res);
9907 382 return res;
9908 }
9909
9910 1 longlong Item_cache_decimal::val_int() {
9911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(fixed);
9912 longlong res;
9913
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!has_value()) return 0;
9914
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &res);
9915 1 return res;
9916 }
9917
9918 394 String *Item_cache_decimal::val_str(String *str) {
9919
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 394 times.
394 assert(fixed);
9920
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 394 times.
394 if (!has_value()) return nullptr;
9921 394 my_decimal_round(E_DEC_FATAL_ERROR, &decimal_value, decimals, false,
9922 &decimal_value);
9923 394 my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, str);
9924 394 return str;
9925 }
9926
9927 260336 my_decimal *Item_cache_decimal::val_decimal(my_decimal *) {
9928
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 260336 times.
260336 assert(fixed);
9929
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 260312 times.
260336 if (!has_value()) return nullptr;
9930 260312 return &decimal_value;
9931 }
9932
9933 105381 bool Item_cache_str::cache_value() {
9934
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 105381 times.
105381 if (!example) return false;
9935 105381 value_cached = true;
9936 105381 value_buff.set(buffer, sizeof(buffer), example->collation.collation);
9937 105381 value = example->val_str(&value_buff);
9938
2/2
✓ Branch 0 taken 2181 times.
✓ Branch 1 taken 103200 times.
105381 if ((null_value = example->null_value))
9939 2181 value = nullptr;
9940
5/6
✓ Branch 0 taken 103200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 101452 times.
✓ Branch 3 taken 1748 times.
✓ Branch 4 taken 101452 times.
✓ Branch 5 taken 1748 times.
103200 else if (value != nullptr && value->ptr() != buffer) {
9941 /*
9942 We copy string value to avoid changing value if 'item' is table field
9943 in queries like following (where t1.c is varchar):
9944 select a,
9945 (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a'),
9946 (select c from t1 where a=t2.a)
9947 from t2;
9948 */
9949 101452 value_buff.copy(*value);
9950 101452 value = &value_buff;
9951 }
9952 105381 return true;
9953 }
9954
9955 void Item_cache_str::store_value(Item *expr, String &s) {
9956 value_cached = true;
9957 if ((null_value = expr->null_value))
9958 value = nullptr;
9959 else {
9960 value_buff.copy(s);
9961 value = &value_buff;
9962 }
9963 }
9964
9965 479 double Item_cache_str::val_real() {
9966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 479 times.
479 assert(fixed == 1);
9967 int err_not_used;
9968 const char *end_not_used;
9969
3/4
✓ Branch 0 taken 479 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 425 times.
479 if (!has_value()) return 0.0;
9970
1/2
✓ Branch 0 taken 425 times.
✗ Branch 1 not taken.
425 if (value)
9971
1/2
✓ Branch 0 taken 425 times.
✗ Branch 1 not taken.
425 return my_strntod(value->charset(), value->ptr(), value->length(),
9972 &end_not_used, &err_not_used);
9973 return (double)0;
9974 }
9975
9976 longlong Item_cache_str::val_int() {
9977 assert(fixed == 1);
9978 int err;
9979 if (!has_value()) return 0;
9980 if (value)
9981 return my_strntoll(value->charset(), value->ptr(), value->length(), 10,
9982 nullptr, &err);
9983 else
9984 return (longlong)0;
9985 }
9986
9987 1260794 String *Item_cache_str::val_str(String *) {
9988
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1260794 times.
1260794 assert(fixed == 1);
9989
2/2
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 1260688 times.
1260794 if (!has_value()) return nullptr;
9990 1260688 return value;
9991 }
9992
9993 1 my_decimal *Item_cache_str::val_decimal(my_decimal *decimal_val) {
9994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(fixed == 1);
9995
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!has_value()) return nullptr;
9996
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (value)
9997 1 str2my_decimal(E_DEC_FATAL_ERROR, value->ptr(), value->length(),
9998 1 value->charset(), decimal_val);
9999 else
10000 decimal_val = nullptr;
10001 1 return decimal_val;
10002 }
10003
10004 4376 type_conversion_status Item_cache_str::save_in_field_inner(
10005 Field *field, bool no_conversions) {
10006
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4374 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4376 times.
4376 if (!value_cached && !cache_value())
10007 return TYPE_ERR_BAD_VALUE; // Fatal: couldn't cache the value
10008
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4375 times.
4376 if (null_value)
10009 1 return set_field_to_null_with_conversions(field, no_conversions);
10010 const type_conversion_status res =
10011 4375 Item_cache::save_in_field_inner(field, no_conversions);
10012
6/8
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 4291 times.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 84 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 84 times.
✓ Branch 7 taken 4291 times.
4459 if (is_varbinary && field->type() == MYSQL_TYPE_STRING && value != nullptr &&
10013
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 value->length() < field->field_length)
10014 84 return TYPE_WARN_OUT_OF_RANGE;
10015 4291 return res;
10016 }
10017
10018 2677 bool Item_cache_row::allocate(uint num) {
10019 2677 item_count = num;
10020 2677 THD *thd = current_thd;
10021 5354 return (!(values = (Item_cache **)thd->mem_calloc(sizeof(Item_cache *) *
10022 2677 item_count)));
10023 }
10024
10025 4928 bool Item_cache_row::setup(Item *item) {
10026 4928 example = item;
10027
4/6
✓ Branch 0 taken 2677 times.
✓ Branch 1 taken 2251 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2677 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4928 times.
4928 if (!values && allocate(item->cols())) return true;
10028
2/2
✓ Branch 0 taken 10398 times.
✓ Branch 1 taken 4928 times.
15326 for (uint i = 0; i < item_count; i++) {
10029 10398 Item *el = item->element_index(i);
10030 Item_cache *tmp;
10031
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10398 times.
10398 if (!(tmp = values[i] = Item_cache::get_cache(el))) return true;
10032 10398 tmp->setup(el);
10033 10398 add_accum_properties(tmp);
10034 }
10035 4928 return false;
10036 }
10037
10038 16220 void Item_cache_row::store(Item *item) {
10039 16220 example = item;
10040
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16220 times.
16220 if (!item) {
10041 assert(is_nullable());
10042 null_value = true;
10043 return;
10044 }
10045
2/2
✓ Branch 0 taken 33692 times.
✓ Branch 1 taken 16220 times.
49912 for (uint i = 0; i < item_count; i++)
10046 33692 values[i]->store(item->element_index(i));
10047 }
10048
10049 7578 bool Item_cache_row::cache_value() {
10050
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7578 times.
7578 if (!example) return false;
10051 7578 value_cached = true;
10052 7578 example->bring_value();
10053 7578 null_value = example->null_value;
10054
10055 const bool cached_item_is_assigned =
10056
4/4
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 7496 times.
✓ Branch 2 taken 65 times.
✓ Branch 3 taken 17 times.
7660 example->type() != SUBSELECT_ITEM ||
10057 82 down_cast<Item_subselect *>(example)->assigned();
10058
10059
2/2
✓ Branch 0 taken 15701 times.
✓ Branch 1 taken 7578 times.
23279 for (uint i = 0; i < item_count; i++) {
10060
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 15667 times.
15701 if (!cached_item_is_assigned) {
10061 // Subquery with zero rows, so make cached item null also.
10062 34 values[i]->store_null();
10063 } else {
10064 15667 values[i]->cache_value();
10065 }
10066
10067 15701 null_value |= values[i]->null_value;
10068 }
10069 7578 return true;
10070 }
10071
10072 void Item_cache_row::illegal_method_call(const char *method
10073 [[maybe_unused]]) const {
10074 DBUG_TRACE;
10075 DBUG_PRINT("error", ("!!! %s method was called for row item", method));
10076 assert(0);
10077 my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
10078 }
10079
10080 bool Item_cache_row::check_cols(uint c) {
10081 if (c != item_count) {
10082 my_error(ER_OPERAND_COLUMNS, MYF(0), c);
10083 return true;
10084 }
10085 return false;
10086 }
10087
10088 bool Item_cache_row::null_inside() {
10089 for (uint i = 0; i < item_count; i++) {
10090 if (values[i]->cols() > 1) {
10091 if (values[i]->null_inside()) return true;
10092 } else {
10093 if (values[i]->update_null_value() || values[i]->null_value) return true;
10094 }
10095 }
10096 return false;
10097 }
10098
10099 449 void Item_cache_row::bring_value() {
10100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 449 times.
449 if (!example) return;
10101 449 example->bring_value();
10102 449 null_value = example->null_value;
10103
2/2
✓ Branch 0 taken 449 times.
✓ Branch 1 taken 449 times.
898 for (uint i = 0; i < item_count; i++) values[i]->bring_value();
10104 }
10105
10106 88488 Item_aggregate_type::Item_aggregate_type(THD *thd, Item *item)
10107 88488 : Item(thd, item) {
10108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88488 times.
88488 assert(item->fixed);
10109 88488 set_nullable(item->is_nullable());
10110
1/2
✓ Branch 0 taken 88488 times.
✗ Branch 1 not taken.
88488 set_data_type(real_data_type(item));
10111
1/2
✓ Branch 0 taken 88488 times.
✗ Branch 1 not taken.
88488 set_typelib(item);
10112
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 88402 times.
88488 if (item->data_type() == MYSQL_TYPE_GEOMETRY)
10113
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 geometry_type = item->get_geometry_type();
10114 else
10115 88402 geometry_type = Field::GEOM_GEOMETRY;
10116 88488 }
10117
10118 /**
10119 Return expression type of Item_aggregate_type.
10120
10121 @return
10122 Item_result (type of internal MySQL expression result)
10123 */
10124
10125 518228 Item_result Item_aggregate_type::result_type() const {
10126 518228 return Field::result_merge_type(data_type());
10127 }
10128
10129 /**
10130 Find real data type of item.
10131
10132 @return
10133 data type which should be used to store item value
10134 */
10135
10136 14085306 static enum_field_types real_data_type(Item *item) {
10137 14085306 item = item->real_item();
10138
10139
4/4
✓ Branch 0 taken 4037472 times.
✓ Branch 1 taken 12526 times.
✓ Branch 2 taken 4844420 times.
✓ Branch 3 taken 5190889 times.
14085306 switch (item->type()) {
10140 4037472 case Item::FIELD_ITEM: {
10141 /*
10142 Item_fields::field_type ask Field_type() but sometimes field return
10143 a different type, like for enum/set, so we need to ask real type.
10144 */
10145 4037472 Field *field = ((Item_field *)item)->field;
10146 4037472 enum_field_types type = field->real_type();
10147
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4037468 times.
4037472 if (field->is_created_from_null_item) return MYSQL_TYPE_NULL;
10148 /* work around about varchar type field detection */
10149
4/6
✓ Branch 0 taken 47682 times.
✓ Branch 1 taken 3989786 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 47682 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4037468 times.
4037468 if (type == MYSQL_TYPE_STRING && field->type() == MYSQL_TYPE_VAR_STRING)
10150 return MYSQL_TYPE_VAR_STRING;
10151 4037468 return type;
10152 }
10153 12526 case Item::SUM_FUNC_ITEM: {
10154 /*
10155 Argument of aggregate function sometimes should be asked about field
10156 type
10157 */
10158 12526 Item_sum *item_sum = (Item_sum *)item;
10159
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 12434 times.
12526 if (item_sum->keep_field_type())
10160 92 return real_data_type(item_sum->get_arg(0));
10161 12434 break;
10162 }
10163 4844420 case Item::FUNC_ITEM:
10164
2/2
✓ Branch 0 taken 8653 times.
✓ Branch 1 taken 4835767 times.
4844420 if (((Item_func *)item)->functype() == Item_func::GUSERVAR_FUNC) {
10165 /*
10166 There are work around of problem with changing variable type on the
10167 fly and variable always report "string" as field type to get
10168 acceptable information for client in send_field, so we make field
10169 type from expression type.
10170 */
10171
3/5
✓ Branch 0 taken 8602 times.
✓ Branch 1 taken 47 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
8653 switch (item->result_type()) {
10172 8602 case STRING_RESULT:
10173 8602 return MYSQL_TYPE_VARCHAR;
10174 47 case INT_RESULT:
10175 47 return MYSQL_TYPE_LONGLONG;
10176 case REAL_RESULT:
10177 return MYSQL_TYPE_DOUBLE;
10178 4 case DECIMAL_RESULT:
10179 4 return MYSQL_TYPE_NEWDECIMAL;
10180 case ROW_RESULT:
10181 default:
10182 assert(0);
10183 return MYSQL_TYPE_VARCHAR;
10184 }
10185 }
10186 4835767 break;
10187 5190889 default:
10188 5190889 break;
10189 }
10190 10039090 return item->data_type();
10191 }
10192
10193 /**
10194 Find field type which can carry current Item_aggregate_type type and
10195 type of given Item.
10196
10197 @param thd the thread/connection descriptor
10198 @param item given item to join its parameters with this item ones
10199
10200 @retval
10201 true error - types are incompatible
10202 @retval
10203 false OK
10204 */
10205
10206 224404 bool Item_aggregate_type::join_types(THD *thd, Item *item) {
10207
1/2
✓ Branch 0 taken 224404 times.
✗ Branch 1 not taken.
224404 DBUG_TRACE;
10208
6/10
✓ Branch 0 taken 224404 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 224404 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 162 times.
✓ Branch 5 taken 224242 times.
✓ Branch 6 taken 162 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 162 times.
✗ Branch 9 not taken.
224404 DBUG_PRINT("info:",
10209 ("was type %d len %d, dec %d name %s", data_type(), max_length,
10210 decimals, (item_name.is_set() ? item_name.ptr() : "<NULL>")));
10211
6/10
✓ Branch 0 taken 224404 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 224404 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 162 times.
✓ Branch 5 taken 224242 times.
✓ Branch 6 taken 162 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 162 times.
✗ Branch 9 not taken.
224404 DBUG_PRINT("info:", ("in type %d len %d, dec %d", real_data_type(item),
10212 item->max_length, item->decimals));
10213 /*
10214 aggregate_type() will modify the data type of this item. Create a copy of
10215 this item containing the original data type and other properties to ensure
10216 correct conversion from existing item types to aggregated type.
10217 */
10218
2/4
✓ Branch 0 taken 224404 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 224404 times.
✗ Branch 3 not taken.
224404 Item *item_copy = new Item_metadata_copy(this);
10219
10220 /*
10221 Down the call stack when calling aggregate_string_properties(), we might
10222 end up in THD::change_item_tree() if we for instance need to convert the
10223 character set on one side of a union:
10224
10225 SELECT "foo" UNION SELECT CONVERT("foo" USING utf8mb3);
10226 might be converted into:
10227 SELECT CONVERT("foo" USING utf8mb3) UNION
10228 SELECT CONVERT("foo" USING utf8mb3);
10229
10230 If we are in a prepared statement or a stored routine (any non-conventional
10231 query that needs rollback of any item tree modifications), we need to
10232 remember what Item we changed ("foo" in this case) and where that Item is
10233 located (in the "args" array in this case) so we can roll back the changes
10234 done to the Item tree when the execution is done. When we enter the rollback
10235 code (THD::rollback_item_tree_changes()), the location of the Item need to
10236 be accessible, so that is why the "args" array must be allocated on a
10237 MEM_ROOT and not on the stack. Note that THD::change_item_tree() isn't
10238 necessary, since the Item array we are modifying isn't a part of the
10239 original Item tree.
10240 */
10241
1/2
✓ Branch 0 taken 224404 times.
✗ Branch 1 not taken.
224404 Item **args = new (thd->mem_root) Item *[2] { item_copy, item };
10242
2/4
✓ Branch 0 taken 224404 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 224404 times.
✗ Branch 3 not taken.
224404 aggregate_type(make_array(&args[0], 2));
10243
10244
1/2
✓ Branch 0 taken 224404 times.
✗ Branch 1 not taken.
224404 Item_result merge_type = Field::result_merge_type(data_type());
10245
2/2
✓ Branch 0 taken 203353 times.
✓ Branch 1 taken 21051 times.
224404 if (merge_type == STRING_RESULT) {
10246
3/4
✓ Branch 0 taken 203353 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 203350 times.
203353 if (aggregate_string_properties("UNION", args, 2)) return true;
10247 /*
10248 For geometry columns, we must also merge subtypes. If the
10249 subtypes are different, use GEOMETRY.
10250 */
10251
6/6
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 203257 times.
✓ Branch 2 taken 92 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 64 times.
✓ Branch 5 taken 203286 times.
203535 if (data_type() == MYSQL_TYPE_GEOMETRY &&
10252 93 (item->data_type() != MYSQL_TYPE_GEOMETRY ||
10253
3/4
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
✓ Branch 3 taken 29 times.
92 geometry_type != item->get_geometry_type()))
10254 64 geometry_type = Field::GEOM_GEOMETRY;
10255 } else
10256
1/2
✓ Branch 0 taken 21051 times.
✗ Branch 1 not taken.
21051 aggregate_num_type(merge_type, args, 2);
10257
4/4
✓ Branch 0 taken 80243 times.
✓ Branch 1 taken 144158 times.
✓ Branch 2 taken 2081 times.
✓ Branch 3 taken 78162 times.
224401 set_nullable(is_nullable() || item->is_nullable());
10258
1/2
✓ Branch 0 taken 224401 times.
✗ Branch 1 not taken.
224401 set_typelib(item);
10259
5/8
✓ Branch 0 taken 224401 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 224401 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 162 times.
✓ Branch 5 taken 224239 times.
✓ Branch 6 taken 162 times.
✗ Branch 7 not taken.
224401 DBUG_PRINT("info", ("become type: %d len: %u dec: %u", (int)data_type(),
10260 max_length, (uint)decimals));
10261 224401 return false;
10262 224404 }
10263
10264 /**
10265 Calculate length for merging result for given Item type.
10266
10267 @param item Item for length detection
10268
10269 @return
10270 length
10271 */
10272
10273 uint32 Item_aggregate_type::display_length(Item *item) {
10274 if (item->type() == Item::FIELD_ITEM)
10275 return ((Item_field *)item)->max_disp_length();
10276
10277 switch (item->data_type()) {
10278 case MYSQL_TYPE_DECIMAL:
10279 case MYSQL_TYPE_TIMESTAMP:
10280 case MYSQL_TYPE_DATE:
10281 case MYSQL_TYPE_TIME:
10282 case MYSQL_TYPE_DATETIME:
10283 case MYSQL_TYPE_YEAR:
10284 case MYSQL_TYPE_NEWDATE:
10285 case MYSQL_TYPE_VARCHAR:
10286 case MYSQL_TYPE_BIT:
10287 case MYSQL_TYPE_NEWDECIMAL:
10288 case MYSQL_TYPE_ENUM:
10289 case MYSQL_TYPE_SET:
10290 case MYSQL_TYPE_TINY_BLOB:
10291 case MYSQL_TYPE_MEDIUM_BLOB:
10292 case MYSQL_TYPE_LONG_BLOB:
10293 case MYSQL_TYPE_BLOB:
10294 case MYSQL_TYPE_VAR_STRING:
10295 case MYSQL_TYPE_STRING:
10296 case MYSQL_TYPE_GEOMETRY:
10297 case MYSQL_TYPE_JSON:
10298 return item->max_length;
10299 case MYSQL_TYPE_BOOL:
10300 return 5;
10301 case MYSQL_TYPE_TINY:
10302 return 4;
10303 case MYSQL_TYPE_SHORT:
10304 return 6;
10305 case MYSQL_TYPE_LONG:
10306 return MY_INT32_NUM_DECIMAL_DIGITS;
10307 case MYSQL_TYPE_FLOAT:
10308 return 25;
10309 case MYSQL_TYPE_DOUBLE:
10310 return 53;
10311 case MYSQL_TYPE_NULL:
10312 return 0;
10313 case MYSQL_TYPE_LONGLONG:
10314 return 20;
10315 case MYSQL_TYPE_INT24:
10316 return 8;
10317 case MYSQL_TYPE_INVALID:
10318 default:
10319 assert(0); // we should never go there
10320 return 0;
10321 }
10322 }
10323
10324 /**
10325 Make temporary table field according collected information about type
10326 of UNION result.
10327
10328 @param table temporary table for which we create fields
10329 @param strict If strict mode is on
10330
10331 @return
10332 created field
10333 */
10334
10335 170592 Field *Item_aggregate_type::make_field_by_type(TABLE *table, bool strict) {
10336 /*
10337 The field functions defines a field to be not null if null_ptr is not 0
10338 */
10339 Field *field;
10340
10341
3/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 170552 times.
170592 switch (data_type()) {
10342 4 case MYSQL_TYPE_ENUM:
10343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 assert(m_typelib != nullptr);
10344
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
12 field = new (*THR_MALLOC)
10345 4 Field_enum(max_length, is_nullable(), item_name.ptr(),
10346 4 get_enum_pack_length(m_typelib->count), m_typelib,
10347
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 collation.collation);
10348
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (field) field->init(table);
10349 4 break;
10350 case MYSQL_TYPE_SET:
10351 assert(m_typelib != nullptr);
10352 field = new (*THR_MALLOC)
10353 Field_set(max_length, is_nullable(), item_name.ptr(),
10354 get_set_pack_length(m_typelib->count), m_typelib,
10355 collation.collation);
10356 if (field) field->init(table);
10357 break;
10358 36 case MYSQL_TYPE_NULL:
10359 36 field = make_string_field(table);
10360 36 break;
10361 170552 default:
10362 170552 field = tmp_table_field_from_field_type(table, false);
10363 170552 break;
10364 }
10365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 170592 times.
170592 if (field == nullptr) return nullptr;
10366
10367
6/6
✓ Branch 0 taken 56186 times.
✓ Branch 1 taken 114406 times.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 56147 times.
✓ Branch 4 taken 18 times.
✓ Branch 5 taken 170574 times.
170631 if (strict && is_temporal_type_with_date(field->type()) &&
10368
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 21 times.
39 !field->is_nullable()) {
10369 /*
10370 This function is used for CREATE SELECT UNION [ALL] ... , and, if
10371 expression is non-nullable, the resulting column is declared
10372 non-nullable with a default of 0. However, in strict mode, for dates,
10373 0000-00-00 is invalid; in that case, don't give any default.
10374 */
10375 18 field->set_flag(NO_DEFAULT_VALUE_FLAG);
10376 }
10377 170592 field->set_derivation(collation.derivation);
10378 170592 return field;
10379 }
10380
10381 /**
10382 Set typelib information for an aggregated enum/set field.
10383 Aggregation of typelib information is possible only if there is a single
10384 underlying item with type enum/set, all other items must be the NULL value.
10385 Aggregation is performed by calling this function repeatedly for each
10386 underlying item.
10387
10388 @param item Item for information collection
10389 */
10390 312889 void Item_aggregate_type::set_typelib(Item *item) {
10391
6/6
✓ Branch 0 taken 312356 times.
✓ Branch 1 taken 533 times.
✓ Branch 2 taken 312351 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 312351 times.
✓ Branch 5 taken 538 times.
312889 if (data_type() != MYSQL_TYPE_ENUM && data_type() != MYSQL_TYPE_SET) return;
10392
10393 // Check that only one underlying item is not the NULL value
10394
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 537 times.
538 if (m_typelib != nullptr) {
10395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(real_data_type(item) == MYSQL_TYPE_NULL);
10396 } else {
10397
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 532 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
537 assert(real_data_type(item) == MYSQL_TYPE_ENUM ||
10398 real_data_type(item) == MYSQL_TYPE_SET);
10399 537 m_typelib = item->get_typelib();
10400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 537 times.
537 assert(m_typelib != nullptr);
10401 }
10402 }
10403
10404 double Item_type_holder::val_real() {
10405 assert(0); // should never be called
10406 return 0.0;
10407 }
10408
10409 longlong Item_type_holder::val_int() {
10410 assert(0); // should never be called
10411 return 0;
10412 }
10413
10414 my_decimal *Item_type_holder::val_decimal(my_decimal *) {
10415 assert(0); // should never be called
10416 return nullptr;
10417 }
10418
10419 String *Item_type_holder::val_str(String *) {
10420 assert(0); // should never be called
10421 return nullptr;
10422 }
10423
10424 bool Item_type_holder::get_date(MYSQL_TIME *, my_time_flags_t) {
10425 assert(0);
10426 return true;
10427 }
10428
10429 bool Item_type_holder::get_time(MYSQL_TIME *) {
10430 assert(0);
10431 return true;
10432 }
10433
10434 138 type_conversion_status Item_values_column::save_in_field_inner(
10435 Field *to, bool no_conversions) {
10436 type_conversion_status res;
10437 138 res = m_value_ref->save_in_field(to, no_conversions);
10438 138 null_value = m_value_ref->null_value;
10439 138 return res;
10440 }
10441
10442 160 Item_values_column::Item_values_column(THD *thd, Item *ref) : super(thd, ref) {
10443 160 fixed = true;
10444 160 }
10445
10446 /* purecov: begin deadcode */
10447
10448 bool Item_values_column::eq(const Item *item, bool binary_cmp) const {
10449 assert(false);
10450 const Item *it = item->real_item();
10451 return m_value_ref && m_value_ref->eq(it, binary_cmp);
10452 }
10453
10454 /* purecov: end */
10455
10456 2 double Item_values_column::val_real() {
10457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 assert(fixed);
10458 2 double tmp = m_value_ref->val_real();
10459 2 null_value = m_value_ref->null_value;
10460 2 return tmp;
10461 }
10462
10463 163 longlong Item_values_column::val_int() {
10464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 163 times.
163 assert(fixed);
10465 163 longlong tmp = m_value_ref->val_int();
10466 163 null_value = m_value_ref->null_value;
10467 163 return tmp;
10468 }
10469
10470 /* purecov: begin deadcode */
10471
10472 my_decimal *Item_values_column::val_decimal(my_decimal *decimal_value) {
10473 assert(false);
10474 assert(fixed);
10475 my_decimal *val = m_value_ref->val_decimal(decimal_value);
10476 null_value = m_value_ref->null_value;
10477 return val;
10478 }
10479
10480 bool Item_values_column::val_bool() {
10481 assert(false);
10482 assert(fixed);
10483 bool tmp = m_value_ref->val_bool();
10484 null_value = m_value_ref->null_value;
10485 return tmp;
10486 }
10487
10488 bool Item_values_column::val_json(Json_wrapper *result) {
10489 assert(false);
10490 assert(fixed);
10491 bool ok = m_value_ref->val_json(result);
10492 null_value = m_value_ref->null_value;
10493 return ok;
10494 }
10495
10496 /* purecov: end */
10497
10498 8 String *Item_values_column::val_str(String *tmp) {
10499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 assert(fixed);
10500 8 tmp = m_value_ref->val_str(tmp);
10501 8 null_value = m_value_ref->null_value;
10502 8 return tmp;
10503 }
10504
10505 10 bool Item_values_column::is_null() {
10506
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 assert(fixed);
10507 /*
10508 Item_values_column is dualistic in nature: It represents both a set
10509 of values, and, during evaluation, an individual value in this set.
10510 This assert will ensure that we only check nullability of individual
10511 values, since a set of values is never NULL. Note that setting
10512 RAND_TABLE_BIT in the constructor prevents this function from being called
10513 during resolving.
10514 */
10515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 assert(m_value_ref != nullptr);
10516 10 bool tmp = m_value_ref->is_null();
10517 10 null_value = m_value_ref->null_value;
10518 10 return tmp;
10519 }
10520
10521 4 bool Item_values_column::get_date(MYSQL_TIME *ltime,
10522 my_time_flags_t fuzzydate) {
10523
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 assert(fixed);
10524 4 bool result = m_value_ref->get_date(ltime, fuzzydate);
10525 4 null_value = m_value_ref->null_value;
10526 4 return result;
10527 }
10528
10529 2 bool Item_values_column::get_time(MYSQL_TIME *ltime) {
10530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 assert(fixed);
10531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 assert(m_value_ref != nullptr);
10532 2 return m_value_ref->get_time(ltime);
10533 }
10534
10535 344 void Item_values_column::add_used_tables(Item *value) {
10536 344 m_aggregated_used_tables |= value->used_tables();
10537 344 }
10538
10539 88310489 void Item_result_field::cleanup() {
10540
1/2
✓ Branch 0 taken 88329179 times.
✗ Branch 1 not taken.
88310489 DBUG_TRACE;
10541 88329179 Item::cleanup();
10542 88327380 result_field = nullptr;
10543 88327380 }
10544
10545 287 void Item_result_field::raise_numeric_overflow(const char *type_name) {
10546 char buf[256];
10547 287 String str(buf, sizeof(buf), system_charset_info);
10548 287 str.length(0);
10549
2/4
✓ Branch 0 taken 287 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 287 times.
✗ Branch 3 not taken.
287 print(current_thd, &str, QT_NO_DATA_EXPANSION);
10550
1/2
✓ Branch 0 taken 287 times.
✗ Branch 1 not taken.
287 str.append('\0');
10551
1/2
✓ Branch 0 taken 287 times.
✗ Branch 1 not taken.
287 my_error(ER_DATA_OUT_OF_RANGE, MYF(0), type_name, str.ptr());
10552 287 }
10553
10554 /**
10555 Helper method: Convert string to the given charset, then print.
10556
10557 @param from_str String to be converted.
10558 @param to_str Query string.
10559 @param to_cs Character set to which the string is to be converted.
10560 */
10561 1177328 void convert_and_print(const String *from_str, String *to_str,
10562 const CHARSET_INFO *to_cs) {
10563
2/2
✓ Branch 0 taken 731780 times.
✓ Branch 1 taken 445548 times.
1177328 if (my_charset_same(from_str->charset(), to_cs)) {
10564 731780 from_str->print(to_str); // already in to_cs, no need to convert
10565 } else // need to convert
10566 {
10567
1/2
✓ Branch 0 taken 445548 times.
✗ Branch 1 not taken.
445548 THD *thd = current_thd;
10568 LEX_STRING lex_str;
10569
1/2
✓ Branch 0 taken 445548 times.
✗ Branch 1 not taken.
445548 thd->convert_string(&lex_str, to_cs, from_str->ptr(), from_str->length(),
10570 from_str->charset());
10571 445548 String tmp(lex_str.str, lex_str.length, to_cs);
10572
1/2
✓ Branch 0 taken 445548 times.
✗ Branch 1 not taken.
445548 tmp.print(to_str);
10573 445548 }
10574 1177328 }
10575
10576 /**
10577 Tells if this is a column of a table whose qualifying query block is 'sl'.
10578 I.e. Item_field or Item_view_ref resolved in 'sl'. Used for
10579 aggregate checks.
10580
10581 @note This returns false for an alias to a SELECT list expression,
10582 even though the SELECT list expression might itself be a column of the
10583 @<table expression@>; i.e. when the function runs on "foo" in HAVING of
10584 "select t1.a as foo from t1 having foo @> 1", it returns false. First, it
10585 pedantically makes sense: "foo" in HAVING is a reference to a column of the
10586 @<query expression@>, not of the @<table expression@>. Second, this behaviour
10587 makes sense for our purpose:
10588 - This is an alias to a SELECT list expression.
10589 - If doing DISTINCT-related checks, this alias can be ignored.
10590 - If doing GROUP-BY-related checks, the aliased expression was already
10591 checked when we checked the SELECT list, so can be ignored.
10592
10593 @retval true3 yes
10594 @retval false3 no
10595 @retval unknown3 it's a non-direct-view Item_ref, we don't know if it
10596 contains a column => caller please analyze "*ref"
10597 */
10598 243011 Bool3 Item_ident::local_column(const Query_block *sl) const
10599
10600 {
10601
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 243011 times.
243011 assert(fixed);
10602
2/2
✓ Branch 0 taken 451 times.
✓ Branch 1 taken 242560 times.
243011 if (m_alias_of_expr) return Bool3::false3();
10603 242560 const Type t = type();
10604
6/6
✓ Branch 0 taken 221915 times.
✓ Branch 1 taken 20645 times.
✓ Branch 2 taken 221906 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 21902 times.
✓ Branch 5 taken 220658 times.
464466 if (t == FIELD_ITEM ||
10605 221906 (t == REF_ITEM &&
10606
2/2
✓ Branch 0 taken 1257 times.
✓ Branch 1 taken 220649 times.
221906 static_cast<const Item_ref *>(this)->ref_type() == Item_ref::VIEW_REF)) {
10607
2/2
✓ Branch 0 taken 1130 times.
✓ Branch 1 taken 20772 times.
21902 if (depended_from) // outer reference
10608 {
10609
2/2
✓ Branch 0 taken 1045 times.
✓ Branch 1 taken 85 times.
1130 if (depended_from == sl)
10610 1045 return Bool3::true3(); // qualifying query is 'sl'
10611
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 20730 times.
20772 } else if (context == nullptr) {
10612 /*
10613 Must be an underlying column of a generated column
10614 as we've dove so deep, we know the gcol is local to 'sl', and so is
10615 this column.
10616 */
10617
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 assert(t == FIELD_ITEM);
10618 42 return Bool3::true3();
10619
2/2
✓ Branch 0 taken 17880 times.
✓ Branch 1 taken 2850 times.
20730 } else if (context->query_block == sl)
10620 17880 return Bool3::true3(); // qualifying query is 'sl'
10621
2/2
✓ Branch 0 taken 220649 times.
✓ Branch 1 taken 9 times.
220658 } else if (t == REF_ITEM) {
10622 /*
10623 We also know that this is not an alias. Must be an internal Item_ref
10624 (like Item_aggregate_ref, Item_outer_ref), go down into it:
10625 */
10626 220649 return Bool3::unknown3();
10627 }
10628 2944 return Bool3::false3();
10629 }
10630
10631 272 bool Item_ident::aggregate_check_distinct(uchar *arg) {
10632 272 Distinct_check *const dc = reinterpret_cast<Distinct_check *>(arg);
10633
10634
2/2
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 109 times.
272 if (dc->is_stopped(this)) return false;
10635
10636 109 Query_block *const sl = dc->select;
10637
1/2
✓ Branch 0 taken 109 times.
✗ Branch 1 not taken.
109 const Bool3 local = local_column(sl);
10638
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 96 times.
109 if (local.is_false()) {
10639 // not a column => ignored, skip child. Other tree parts deserve checking.
10640 13 dc->stop_at(this);
10641 13 return false;
10642 }
10643
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 84 times.
96 if (local.is_unknown()) return false; // dive in child item
10644
10645 /*
10646 Point (2) of Distinct_check::check_query() is true: column is
10647 from table whose qualifying query block is 'sl'.
10648 */
10649 uint counter;
10650 enum_resolution_type resolution;
10651
2/4
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
84 Item **const res = find_item_in_list(current_thd, this, &sl->fields, &counter,
10652 REPORT_EXCEPT_NOT_FOUND, &resolution);
10653
10654
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 24 times.
84 if (res == not_found_item) {
10655 /*
10656 Point (3) of Distinct_check::check_query() is true: column is
10657 not in SELECT list.
10658 */
10659 60 dc->failed_ident = this;
10660 // Abort processing of the entire item tree.
10661 60 return true;
10662 }
10663 /*
10664 If success, do not dive in the child either! Indeed if this is
10665 Item_.*view_ref to an expression coming from a merged view, we mustn't
10666 check its underlying base-table columns, it may give false errors,
10667 consider:
10668 create view v as select x*2 as b from ...;
10669 select distinct b from v order by b+1;
10670 'b' of ORDER BY is in SELECT list so query is valid, we mustn't check
10671 the underlying 'x' (which is not in SELECT list).
10672 */
10673 24 dc->stop_at(this);
10674 24 return false;
10675 }
10676
10677 496693 bool Item_ident::aggregate_check_group(uchar *arg) {
10678 496693 Group_check *const gc = reinterpret_cast<Group_check *>(arg);
10679 496693 return gc->do_ident_check(this, 0, Group_check::CHECK_GROUP);
10680 }
10681
10682 286 bool Item_ident::is_strong_side_column_not_in_fd(uchar *arg) {
10683 286 std::pair<Group_check *, table_map> *p =
10684 reinterpret_cast<std::pair<Group_check *, table_map> *>(arg);
10685 // p->first is Group_check, p->second is map of strong tables.
10686 286 return p->first->do_ident_check(this, p->second,
10687 286 Group_check::CHECK_STRONG_SIDE_COLUMN);
10688 }
10689
10690 79 bool Item_ident::is_column_not_in_fd(uchar *arg) {
10691 79 Group_check *const gc = reinterpret_cast<Group_check *>(arg);
10692 79 return gc->do_ident_check(this, 0, Group_check::CHECK_COLUMN);
10693 }
10694
10695 /**
10696 The aim here is to find a real_item() which is of type Item_field.
10697 */
10698 219 bool Item_ref::repoint_const_outer_ref(uchar *arg) {
10699 219 *(pointer_cast<bool *>(arg)) = true;
10700 219 return false;
10701 }
10702
10703 /**
10704 If this object is the real_item of an Item_ref, repoint the result_field to
10705 field.
10706 */
10707 531 bool Item_field::repoint_const_outer_ref(uchar *arg) {
10708 531 bool *is_outer_ref = pointer_cast<bool *>(arg);
10709
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 439 times.
531 if (*is_outer_ref) result_field = field;
10710 531 *is_outer_ref = false;
10711 531 return false;
10712 }
10713
10714 /**
10715 Generated fields don't need db/table names. Strip them off as inplace ALTER
10716 can reallocate them, making pointers invalid.
10717 */
10718 75419 bool Item_field::strip_db_table_name_processor(uchar *) {
10719 75419 db_name = nullptr;
10720 75419 table_name = nullptr;
10721 75419 return false;
10722 }
10723
10724 7531 string ItemToString(const Item *item) {
10725
3/4
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 7472 times.
✓ Branch 2 taken 59 times.
✗ Branch 3 not taken.
7531 if (item == nullptr) return "(none)";
10726 7472 String str;
10727
1/2
✓ Branch 0 taken 7472 times.
✗ Branch 1 not taken.
7472 const ulonglong save_bits = current_thd->variables.option_bits;
10728
1/2
✓ Branch 0 taken 7472 times.
✗ Branch 1 not taken.
7472 current_thd->variables.option_bits &= ~OPTION_QUOTE_SHOW_CREATE;
10729 14944 item->print(
10730
2/4
✓ Branch 0 taken 7472 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7472 times.
✗ Branch 3 not taken.
7472 current_thd, &str,
10731 enum_query_type(QT_NO_DEFAULT_DB | QT_SUBSELECT_AS_ONLY_SELECT_NUMBER));
10732
1/2
✓ Branch 0 taken 7472 times.
✗ Branch 1 not taken.
7472 current_thd->variables.option_bits = save_bits;
10733
1/2
✓ Branch 0 taken 7472 times.
✗ Branch 1 not taken.
7472 return to_string(str);
10734 7472 }
10735
10736 368 Item_field *FindEqualField(Item_field *item_field, table_map reachable_tables,
10737 bool replace, bool *found) {
10738
2/2
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 249 times.
368 if (item_field->item_equal_all_join_nests == nullptr) {
10739 119 *found = false;
10740 119 return item_field;
10741 }
10742
10743 // We have established in
10744 // 'Item_func_eq::ensure_multi_equality_fields_are_available' that this
10745 // item references a field that is outside of our reach. We also have a
10746 // multi-equality (item_equal_all_join_nests is set), so we go through all
10747 // fields in the multi-equality and find the first that is within our reach.
10748 // The table_map provided in 'reachable_tables' defines the tables within our
10749 // reach.
10750 249 for (Item_field &other_item_field :
10751
5/8
✓ Branch 0 taken 249 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 615 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 539 times.
✓ Branch 7 taken 76 times.
864 item_field->item_equal_all_join_nests->get_fields()) {
10752
2/2
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 290 times.
539 if (other_item_field.field == item_field->field) {
10753 249 continue;
10754 }
10755
10756
1/2
✓ Branch 0 taken 290 times.
✗ Branch 1 not taken.
290 table_map item_field_used_tables = other_item_field.used_tables();
10757
2/2
✓ Branch 0 taken 173 times.
✓ Branch 1 taken 117 times.
290 if ((item_field_used_tables & reachable_tables) == item_field_used_tables) {
10758 173 *found = true;
10759
2/2
✓ Branch 0 taken 170 times.
✓ Branch 1 taken 3 times.
173 if (replace) {
10760
3/6
✓ Branch 0 taken 170 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 170 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 170 times.
✗ Branch 5 not taken.
170 Item_field *new_item_field = new Item_field(current_thd, item_field);
10761
1/2
✓ Branch 0 taken 170 times.
✗ Branch 1 not taken.
170 new_item_field->reset_field(other_item_field.field);
10762 173 return new_item_field;
10763 } else {
10764 3 return item_field;
10765 }
10766 }
10767 }
10768 76 *found = false;
10769 76 return item_field;
10770 }
10771
10772 700252 bool Item_asterisk::itemize(Parse_context *pc, Item **res) {
10773
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 700252 times.
700252 assert(pc->select->parsing_place == CTX_SELECT_LIST);
10774
10775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 700252 times.
700252 if (skip_itemize(res)) {
10776 return false;
10777 }
10778
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 700252 times.
700252 if (super::itemize(pc, res)) {
10779 return true;
10780 }
10781 700252 pc->select->with_wild++;
10782 700252 return false;
10783 }
10784
10785 325375 bool ItemsAreEqual(const Item *a, const Item *b, bool binary_cmp) {
10786 325375 const Item *real_a = a->real_item();
10787 325375 const Item *real_b = b->real_item();
10788
10789 // Unwrap caches, as they may not be added consistently
10790 // to both sides.
10791
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 325375 times.
325375 if (real_a->type() == Item::CACHE_ITEM) {
10792 real_a = down_cast<const Item_cache *>(real_a)->get_example();
10793 }
10794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 325375 times.
325375 if (real_b->type() == Item::CACHE_ITEM) {
10795 real_b = down_cast<const Item_cache *>(real_b)->get_example();
10796 }
10797 325375 return real_a->eq(real_b, binary_cmp);
10798 }
10799
10800 190078 bool AllItemsAreEqual(const Item *const *a, const Item *const *b, int num_items,
10801 bool binary_cmp) {
10802
2/2
✓ Branch 0 taken 323740 times.
✓ Branch 1 taken 140528 times.
464268 for (int i = 0; i < num_items; ++i) {
10803
2/2
✓ Branch 0 taken 49550 times.
✓ Branch 1 taken 274190 times.
323740 if (!ItemsAreEqual(a[i], b[i], binary_cmp)) {
10804 49550 return false;
10805 }
10806 }
10807 140528 return true;
10808 }
10809